Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  165] [ 1]  / answers: 1 / hits: 38410  / 13 Years ago, thu, november 24, 2011, 12:00:00

I made a javascript audio test. All the function works in Opera FF and Chrome, except audio.oncanplaythrough, and audio.onended (this 2function dont work on Chrome).



<!DOCTYPE html>
<html>
<body>

<script>
var audio = new Audio(http://www.w3schools.com/html5/song.ogg);

audio.oncanplaythrough = function(){
audio.play();
}

audio.onended = function(){
alert('ended');
}

</script>
<a href=# onclick=audio.play();>start</a><br/>
<a href=# onclick=audio.pause();>pause</a><br/>
<a href=# onclick=audio.volume=prompt('from 0 to 1',0.7)>volume</a><br/>
<a href=# onclick=audio.currentTime = 3;>jump</a><br/>
</body>
</html>

More From » html

 Answers
128

oncanplaythrough is an event, not a method, and the other event is called ended and not onended.



So you need to listen for the events and act on them. Try:



audio.addEventListener('ended', function() { 
alert('ended');
}, false);


and



audio.addEventListener('canplaythrough', function() { 
audio.play();
}, false);

[#88920] Wednesday, November 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;