Turn byte array to sound

醉酒当歌 提交于 2020-01-10 15:27:15

问题


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

Thanks


回答1:


As far as I know this is decidedly non trivial.

  1. You can turn the array into a data URI, and then play it back normally.
  2. You can post it back to a server to do the encoding and play it back normally.
  3. You can use a fancy API

2 seems inefficient, 3 requires browser specific support. So, use 1. I havent tried it, but check out http://www.bitsnbites.eu/?p=1. You should expect this to be way less efficient than native code.




回答2:


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



来源:https://stackoverflow.com/questions/6937917/turn-byte-array-to-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!