I just built a real-time application using socket.io, where the user "master" can play sounds on receiving devices (desktop browsers, mobile browsers). This master user sees a list of sound files and can click "Play" in the sound file.
Sound playback in browsers is instant. However, on mobile phones there is a delay of 0.5-2 seconds (my Nexus 4 and iPhone 5 about 1 second and iPhone 3GS 1-2 seconds).
I tried several things to optimize sound reproduction to make it faster on mobile phones. Right now (in the best "phase" of its optimization, I would say), I am combining all mp3s together into one audio file (it creates .mp3, .ogg and .mp4 files). I need ideas on how I can further fix / improve this problem. The bottleneck really looks like hmtl 5 audio methods like .play() .
In receivers I use as such:
<audio id="audioFile" preload="auto"> <source src="/output.m4a" type="audio/mp4"/> <source src="/output.mp3" type="audio/mpeg"/> <source src="/output.ogg" type="audio/ogg"/> <p>Your browser does not support HTML5 audio.</p> </audio>
In my JS:
var audioFile = document.getElementById('audioFile'); // Little hack for mobile, as only a user generated click will enable us to play the sounds $('#prepareAudioBtn').on('click', function () { $(this).hide(); audioFile.play(); audioFile.pause(); audioFile.currentTime = 0; }); // Master user triggered a sound sprite to play socket.on('playAudio', function (audioClip) { if (audioFile.paused) audioFile.play(); audioFile.currentTime = audioClip.startTime; // checks every 750ms to pause the clip if the endTime has been reached. // There is a second of "silence" between each sound sprite so the pause is sure to happen at a correct time. timeListener(audioClip.endTime); }); function timeListener(clipEndTime) { this.clear = function () { clearInterval(interval); interval = null; }; if (interval !== null) { this.clear(); } interval = setInterval(function () { if (audioFile.currentTime >= clipEndTime) { audioFile.pause(); this.clear(); } }, 750); }
Also considered blob for each sound, but some sounds may take minutes, so I resorted to combining all the sounds together for 1 large audio file (better than several audio tags on the page for each clip)
source share