2016-05-27 59 views
0

我想將一個bytearray解碼爲audiobuffer並使用JS在瀏覽器中播放我的wav文件。 這裏我使用的代碼,但點擊播放按鈕時,我得到一個異常,即「無法解碼音頻數據」。如何將ByteArray轉換爲AudioBuffer並使用Javascript播放

{ 
    if (!window.AudioContext){ 
     if (!window.webkitAudioContext){ 
      alert("Your browser does not support any AudioContext and cannot play back this audio."); 
      return; 
     } 
    } 

    var context = new AudioContext(); 
    var arr = me.queueStore.getAt(0).data.ByteAudio; 
    var arrayBuffer = new ArrayBuffer(arr.length); 
    var bufferView = new Uint8Array(arrayBuffer); 
    for (i = 0; i < arr.length; i++) { 
      bufferView[i] = arr[i]; 
    }  
    context.decodeAudioData(bufferView, function (buffer) { 
    var source = context.createBufferSource(); // creates a sound source 
    source.buffer = buffer;     // tell the source which sound to play 
    source.connect(context.destination);        
    source.start(0); 
    }); 
} 
+0

得到了答案,在這裏這種情況的解決方案:使用64位字符串玩媒體文件,而不是轉換爲音頻緩衝區:me.fileString =「data:audio/wav; base64,」+ data.Tone; var audio = new Audio(fileString); audio.play(); –

回答

0

得到了答案,在這裏這種情況的解決方案:使用64位字符串播放媒體文件,而不是轉換成音頻緩衝: me.fileString = "data:audio/wav;base64," + data.tonebyteArray; var audio = new Audio(fileString); audio.play();