AJAX

2012-09-17 6 views
6

का उपयोग कर एचटीएमएल 5 मीडिया स्रोत सेट करना मुझे अपने ऑडियो फाइलों के लिए प्रमाणीकरण हेडर की सहायता करने की आवश्यकता है, मैं बाहरी सर्वर से हथियार ले रहा था। तो अब मैं AJAX का उपयोग करने की कोशिश कर रहा हूं, मैं फ़ाइलों को ठीक से पकड़ सकता हूं, लेकिन मैं उन्हें अपने प्लेयर के लिए मीडिया स्रोत के रूप में सेट नहीं कर सकता। ऑडियो स्रोत के रूप में AJAX लोड की गई फ़ाइल को सेट करने के लिए आप कैसे पहुंचते हैं?AJAX

संपादित

मामले में किसी को यह तय करने समाप्त वापस इस तरह से आता है।

if (this.mAudioPlayer.canPlayType("audio/mpeg")) { 
    this.mExtension = '.mp3'; 
}else if (this.mAudioPlayer.canPlayType("audio/ogg")) { 
    this.mExtension = '.ogg'; 
} else if (this.mAudioPlayer.canPlayType("audio/mp4")) { 
    this.mExtension = '.m4a'; 
} 

this.CreateAudioData = function() { 

    //downloading audio for use in data:uri 
    $.ajax({ 
     url: aAudioSource + this.mExtension + '.txt', 
     type: 'GET', 
     context: this, 
     async: false, 
     beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', window.userId);}, 
     success: this.EncodeAudioData, 
     error: function(xhr, aStatus, aError) { HandleError('Audio Error: ' + aStatus); } 
    }); 
}; 

this.EncodeAudioData = function(aData) { 
    //this.mAudioData = base64_encode(aData); 
    this.mAudioData = aData; 

    if (this.mExtension == '.m4a') { 
     Debug("playing m4a"); 
     this.mAudioSrc = "data:audio/mp4;base64," + this.mAudioData; 
    } else if (this.mExtension == '.ogg') { 
     Debug("playing ogg"); 
     this.mAudioSrc = "data:audio/ogg;base64," + this.mAudioData; 
    } else if (this.mExtension == '.mp3') { 
     Debug("playing mp3"); 
     this.mAudioSrc = "data:audio/mp3;base64," + this.mAudioData; 
    } 

}; 

this.play = function() { 

    if (this.mAudioPlayer.src != this.mAudioSrc) { 
     this.mAudioPlayer.src = this.mAudioSrc; 
    } 
    this.mAudioPlayer.load(); 
    this.mAudioPlayer.play(); 
}; 

एसिंच करना था: झूठी, अन्यथा मुझे इसके बजाय ऑडियो का एक छोटा हिस्सा मिल जाएगा। हालांकि अंत में एसिंच बनाया डीबगिंग को आसान बना रहा है।

उत्तर

-1
if (this.mAudioPlayer.canPlayType("audio/mpeg")) { 
    this.mExtension = '.mp3'; 
}else if (this.mAudioPlayer.canPlayType("audio/ogg")) { 
    this.mExtension = '.ogg'; 
} else if (this.mAudioPlayer.canPlayType("audio/mp4")) { 
    this.mExtension = '.m4a'; 
} 

this.CreateAudioData = function() { 

//downloading audio for use in data:uri 
$.ajax({ 
    url: aAudioSource + this.mExtension + '.txt', 
    type: 'GET', 
    context: this, 
    async: false, 
    beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', window.userId);}, 
    success: this.EncodeAudioData, 
    error: function(xhr, aStatus, aError) { HandleError('Audio Error: ' + aStatus); } 
    }); 
}; 

this.EncodeAudioData = function(aData) { 
    //this.mAudioData = base64_encode(aData); 
    this.mAudioData = aData; 

    if (this.mExtension == '.m4a') { 
    Debug("playing m4a"); 
    this.mAudioSrc = "data:audio/mp4;base64," + this.mAudioData; 
    } else if (this.mExtension == '.ogg') { 
    Debug("playing ogg"); 
    this.mAudioSrc = "data:audio/ogg;base64," + this.mAudioData; 
    } else if (this.mExtension == '.mp3') { 
    Debug("playing mp3"); 
    this.mAudioSrc = "data:audio/mp3;base64," + this.mAudioData; 
    } 

}; 

this.play = function() { 

    if (this.mAudioPlayer.src != this.mAudioSrc) { 
     this.mAudioPlayer.src = this.mAudioSrc; 
    } 
    this.mAudioPlayer.load(); 
    this.mAudioPlayer.play(); 
}; 

asynch करना था: झूठे, अन्यथा मैं इसे के सभी के बजाय ऑडियो का एक छोटा सा हिस्सा मिलेगा। हालांकि अंत में एसिंच बनाया डीबगिंग को आसान बना रहा है।

4

क्या आप वास्तव में फ़ाइल डाउनलोड कर रहे हैं, या इसे बेस 64 एन्कोडेड प्रारूप (यानी डेटा यूआरआई के रूप में) में वापस कर रहे हैं?

जावास्क्रिप्ट के माध्यम से ऑडियो तत्व के स्रोत को बदलना काफी सरल है।

<audio id="myAudio" controls /> 

और फिर एक बार आप स्रोत है ,:

var audio = document.getElementById("myAudio"); 
audio.src = myAudioFile; 
audio.type = "type/ogg"; // ony showing an OGG example here 
+0

आधार 64 एन्कोडेड स्ट्रिंग के रूप में अजाक्स का उपयोग करके लोड हो रहा है। मैं अपने प्रयास के साथ पोस्ट संपादित करें। – Neablis

+0

मैं उससे पूछने जा रहा था, लेकिन यह भी काम करना चाहिए। –