How to play audio byte array (not file!) with JavaScript in a browser

后端 未结 4 2062
终归单人心
终归单人心 2021-02-08 14:51

For mostly security reasons, I\'m not allowed to store a WAV file on the server to be accessed by a browser. What I have is a byte array contains audio data (the data portion of

4条回答
  •  醉梦人生
    2021-02-08 15:47

    The following code will play the sine wave at 0.5 and 2.0. Call the function play_buffersource() in your button or anywhere you want.

    Tested using Chrome with Web Audio flag enabled. For your case, all that you need to do is just to shuffle your audio bytes to the buf.

    
    

    References:

    • http://epx.com.br/artigos/audioapi.php
    • https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html

    If you need to resample the audio, you can use a JavaScript resampler: https://github.com/grantgalitz/XAudioJS

    If you need to decode the base64 data, there are a lot of JavaScript base64 decoder: https://github.com/carlo/jquery-base64

提交回复
热议问题