I'm working on an audio player using the HTML5 <audio>
element and am trying to iron out a few issues cross-browser.
This player is configured to switch between audio tracks when a link for a different track is clicked. Internally, it pauses the <audio>
element, sets its currentTime
to 0
(as explained in HTML5 audio not playing multiple times in Android 4.0.4 device Native Browser) and removes it from the DOM before a new <audio>
element is created with the new source track specified. The play()
method is called on the element to load the track and start playing.
On desktop browsers, an iPhone and iPad, this works fine. It also works on a colleague's Samsung Galaxy S3 Mini. However on a different test phone, a full-size Samsung Galaxy S3, the playback of subsequent tracks fails - the track won't even play and it shows in the playback bar as having completed. This is also the case on the stock Android 4.1.2 browser on my Motorola Xoom tablet.
The first track specified on page load always works fine.
After adding a few debugging statements among the JavaScript code, particularly the timeupdate
callback function, I have found that in the cases where playback fails, the <audio>
element's duration
has a value of 1 in the two instances when timeupdate
is raised. Normally, the duration is reported as anything from 350 to just over 1000 for a 3-4 min track. This explains why the playback bar immediately showed the newly-loaded track as having completed playback.
I'm thinking that the subsequent track isn't being loaded properly; even if it has been previously loaded and cached on the phone, surely it must be able to be loaded correctly. All tracks are MP3s and are able to be played in the respective browsers.
What am I missing here? Any idea what the problem is and how to get this to work?
Many thanks.
More Info:
I've hooked up other events to the <audio>
element to trace what may be happening:
progress
(with a report on the buffered ranges)loadedmetadata
canplay
error
On desktop (where this works correctly), the events are as follows:
timeupdate
(with NaN duration), progress
x3 (0 buffered ranges), loadedmetadata
, canplay
, timeupdate
with duration of 121.172368, progress
with 1 buffered range [0, 11.507715], followed by a sequence of timeupdate
and progress
events as the track continues to play. The iPhone has a similar sequence of events.
On the Samsung S3, I get the following events: timeupdate
with a duration of 1, followed by progress
(0 buffered ranges), loadedmetadata
, canplay
, and finally a timeupdate
with duration still at 1.
This page has been very useful in explaining the events and data structures: http://www.sitepoint.com/essential-audio-and-video-events-for-html5/