Turn byte array to sound

前端 未结 2 936
慢半拍i
慢半拍i 2021-01-01 03:18

I have an mp3 file as byte array. How to turn it back to a sound and play using javascript?

Thanks

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 04:04

    This is just a follow-up on Philip JF's answer:

    "1" will probably work fine without any of the tricky stuff explained on the bitsnbites link. Since mp3 files are without header, you can pass on the data to the URL "as is", without WAVE header. So the way to go (modified from the bitsnbites page):

    Construct the string to be played as a DATA URI: Initialize a string with "data:audio/mpeg;base64," Append the mp3 byte array as a formatted string in base64 encoding using the btoa() function. Then you can invoke this Data URI in order to play it.

    References:

    https://developer.mozilla.org/en/DOM/window.btoa

    http://en.wikipedia.org/wiki/Data_URI_scheme

提交回复
热议问题