Wednesday, November 30th, -0001
HTML5 Audio Tutorial: Rotating Channels
<p>Thomas Sturm continues to add great content to his HTML5 stories in flight. His latest talks on HTML 5 Audio and goes into a tactic to implement rotating channels:Here is one solution to overcome the single-channel limitation of the audio tag: Use multiple rotating audio channels and assign new sounds to currently unused channels. Click the links above rapidly to test this.
In the example above we use 10 channels (generated audio objects) and whenever the user clicks another sound to play, the script finds an inactive (and therefore unblocked) channel and then loads and plays the selected sound.
Each of the sounds is being preloaded with an audio html tag that is actually never used to play the sound - the autobuffer="autobuffer" property forces the browser to load all of the sounds when the page loads, instead of when the sound is played for the first time through one of the generated audio channels.
The script checks each channel if it is done playing the previous sound. There is an "ended" property for the audio object, but since that property is "false" when new objects are created (which is correct, but inconvenient), I've decided to keep track of the expected end times for each sound channel instead.
HTML:
<audio id="multiaudio1" src="audio/flute_c_long_01.wav" autobuffer="autobuffer"></audio> <audio id="multiaudio2" src="audio/piano_chord.wav" autobuffer="autobuffer"></audio> <audio id="multiaudio3" src="audio/synth_vox.wav" autobuffer="autobuffer"></audio> <audio id="multiaudio4" src="audio/shimmer.wav" autobuffer="autobuffer"></audio> <audio id="multiaudio5" src="audio/sweep.wav" autobuffer="autobuffer"></audio> <script type="text/javascript"> var channel_max = 10; // number of channels audiochannels = new Array(); for (a=0;a<channel_max ;a++) { // prepare the channels audiochannels[a] = new Array(); audiochannels[a]['channel'] = new Audio(); // create a new audio object audiochannels[a]['finished'] = -1; // expected end time for this channel } function play_multi_sound(s) { for (a=0;a<audiochannels.length;a++) { thistime = new Date(); if (audiochannels[a]['finished'] <thistime.getTime()) { // is this channel finished? audiochannels[a]['finished'] = thistime.getTime() + document.getElementById(s).duration*1000; audiochannels[a]['channel'].src = document.getElementById(s).src; audiochannels[a]['channel'].load(); audiochannels[a]['channel'].play(); break; } } } </script>
Related Content:











Leave a comment