Jump to content

Controlling HTML5 audio with Javascript


MuscovyX

Recommended Posts

I'm trying to make something that starts playing a song at a predetermined offset. I've tried Googling numerous uses of Javascript's currentTime, none of which work in Firefox or Chromium. Does anyone know how to use it?Also, there is a second problem that using JavaScript over vanilla HTML5 created - the audio frequently stops within the first 30 seconds. This didn't happen when i used the <audio> tag.

function StartSong() {	var audioElement = document.createElement('audio');	audioElement.setAttribute('src', '/uploads/music/Song.ogg');	audioElement.load() // What does this do? Seems to be unnecessary.	//audioElement.currentTime=35;	audioElement.play();}

Link to comment
Share on other sites

it could be because your calling play() before the audio is loaded, try using a callback like:

function StartSong() {	var audioElement = document.createElement('audio');	audioElement.src = '/uploads/music/Song.ogg';	audioElement.load();	audioElement.oncanplaythrough = function() {		audioElement.currentTime=35;		audioElement.play();	}}

these links may be helpful:HTMLAudioElement - https://developer.mozilla.org/en/DOM/HTMLAudioElementHTMLMediaElement - https://developer.mozilla.org/en/DOM/HTMLMediaElement (load() is documented here)HTML 5 Events - http://www.w3schools.com/html5/html5_ref_eventattributes.asp (the Media Events section)

Link to comment
Share on other sites

it could be because your calling play() before the audio is loaded, try using a callback like:
function StartSong() {	var audioElement = document.createElement('audio');	audioElement.src = '/uploads/music/Song.ogg';	audioElement.load();	audioElement.oncanplaythrough = function() {		audioElement.currentTime=35;		audioElement.play();	}}

these links may be helpful:HTMLAudioElement - https://developer.mozilla.org/en/DOM/HTMLAudioElementHTMLMediaElement - https://developer.mozilla.org/en/DOM/HTMLMediaElement (load() is documented here)HTML 5 Events - http://www.w3schools.com/html5/html5_ref_eventattributes.asp (the Media Events section)

If I do that in Chrome, nothing happens. If I do that in Firefox, it just makes a disc-skipping sound.Edit: also I'm not sure if buffering is a factor, as I'm running it from localhost right now.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...