Skip to content Skip to sidebar Skip to footer

How To Convert Raw Mp3 Binary Into Blob Url And Play It In Audio Tag

What im doing is sending mp3 data through websockets as base64 strings and decoding it on browser side now what im trying to do is turn that raw data into a blob and play it in an

Solution 1:

You don't need to convert it to raw . HTML5 Audio already supports base64 as a URL :

<audio controls src="data:audio/mp3;base64, ...

Solution 2:

You can try using Audiocontext from Web Audio API. Use AudioContext.createBuffer() for raw data and then it can be played by being passed into an AudioBufferSourceNode. You can also use the decodeAudioData() method to use an audio file.

Solution 3:

Not sure what kind of data is "js.buffer"? But if it is base64 encoded already, just prefix it like @Ashraf said as:

var audio = document.getElementById("audio");
audio.type = AudioType;
audio.src = "data:audio/mpeg;base64," + js.buffer;
// audio.src = "data:" + AudioType + ";base64," + js.buffer;

FYI: Modern browsers have built-in base64 encode/decode.

Use:
   atob() - decode base64
   btoa() - encode base64

Post a Comment for "How To Convert Raw Mp3 Binary Into Blob Url And Play It In Audio Tag"