Simple Playback Example 1:
varUrl= '. /content/audio/. mp3 ';if(!window. Audiocontext) {alert (' Your browser does not support Audiocontext ');} Else { //Create Context varCTX =NewAudiocontext (); //using AJAX to get audio files varRequest =NewXMLHttpRequest (); Request.open (' GET ', url,true); Request.responsetype= ' Arraybuffer ';//return type of configuration data //Load CompleteRequest.onload =function () { varArraybuffer =Request.response; Ctx.decodeaudiodata (Arraybuffer,function(buffer) {//processing successfully returned a data type of Audiobufferconsole.info (buffer); //Create a Audiobuffersourcenode object varSource =Ctx.createbuffersource (); Source.buffer=buffer; Source.connect (ctx.destination); //start playback at the specified positionSource.start (0); }, function(e) {console.info (' Processing error '); }); } request.send ();}
Example 2, with pause:
<Divclass= "Container"> <Buttonclass= "Btn btn-primary"ID= "Playbtn"> <Iclass= "Glyphicon glyphicon-pause"></I> </Button></Div><Script> varURL='.. /content/audio/. mp3'; if (!window. Audiocontext) {alert ('your browser does not support Audiocontext'); } Else { //Create Context varCTX= NewAudiocontext (); varSource= NULL; //using AJAX to get audio files varRequest= NewXMLHttpRequest (); Request.open ('GET', URLs,true); Request.responsetype= 'Arraybuffer';//return type of configuration data //Load CompleteRequest.onload= function () { varArraybuffer=Request.response; Ctx.decodeaudiodata (Arraybuffer,function(buffer) {//processing successfully returned a data type of Audiobuffer //console.info (buffer); //Create a Audiobuffersourcenode objectSource=Ctx.createbuffersource (); Source.buffer=buffer; Source.connect (ctx.destination); //start playback at the specified positionSource.start (0); Console.info (source); }, function(e) {console.info ('processing error'); }); } request.send (); //Bind play button $('#playBtn'). Click (function () { varicon= $( This). Find ('I');; Icon.toggleclass ('Glyphicon-play'). Toggleclass ('Glyphicon-pause'); //Stop playingSource.stop (); }); } </Script>
Description
Webaudio API is mainly designed for audio files to add sound effects, but it can also be used to play audio files, which is similar to the function of the HTML5 audio element, but the audio element can have a control interface, the user can click the Play/Stop button on the interface to control the file playback, You can also drag the progress bar on the interface to control the playback progress. Audio playback using the Webaudio API does not have a control interface, but Android,ios is really useful for mobile platforms, For example, on the Android platform, the Chrome browser set the Gesture-requirement-for-media-playback property, which means that audio file playback cannot be achieved by invoking the play function of the audio element. In addition to calling the play function, the user must be asked to have a gesture on the screen that is consistent with Apple's behavior on iOS.
Efficiency issues using Webaudio to play audio files
The previous article describes how to use Webaudio to play audio files, but it is important to note that this feature is not easy to use Webaudio, because when the audio file is large, it may affect the execution efficiency of the program. First, if we use XMLHttpRequest in the program to download the file, this is a more time-consuming operation, depending on the current network environment and the size of the file, although the program uses asynchronous download, but also let the audio playback delay. Second, the program needs to call Webaudio's Decodeaudiodata function to decode the entire audio file, it is important to note that it needs to decode the entire file once, it will trigger a successful callback function, the program can start to play audio files, which adds to the audio file playback delay, In addition, due to the one-time decoding of the entire file, the entire pre-decoding and decoded files are stored in memory simultaneously, which also causes a huge amount of memory overhead (when playing with the audio element because the audio element is decoded while playing). At this point may have a friend will question the implementation of the Decodeaudiodata API is a problem, in fact, the function is to decode a relatively short sound file design, in addition, due to the webaudio of the audio delay of special attention, so for less sound delay, Requires that the audio file to be processed be loaded into memory prior to sound processing.
So if you need to use Webaudio to play the file, but also focus on efficiency issues, it is recommended to reduce the size of the audio files, or decomposition into a number of small files and then load the decoding playback separately.
MORE: HTML5 Webaudioapi introduction (I.)
HTML5 webaudioapi-Example (ii)