简体   繁体   中英

redirecting to a new page when an html 5 audio file has finished playing

I am playing an audiobook chapter with the text and want it to automatically redirect to a new page with the next chapter text and audio when the html5 audio file finishes.

I am not a programmer so I could use some help.

I know I need to use the addEventListener with the ended function similiar to what is below but I do not know how to modify this to call a new page instead of a new audio file.

<audio id="audio" autoplay controls src="song1.mp3" type="audio/mpeg"></audio>
<script type="text/javascript">
    document.getElementById('audio').addEventListener("ended",function() {
    this.src = "song2.mp3?nocache="+new Date().getTime();
    this.play();
    });
</script>

Use the ended event and redirect:

http://www.w3.org/html/wg/drafts/html/master/embedded-content.html#event-media-ended

function redirectHandler() {
  window.location = 'http://example.com';
}

audio.addEventListener('ended', redirectHandler, false);

Demo: http://jsfiddle.net/tqam3zyf/2/

When you use

this.src = "song2.mp3?nocache="+new Date().getTime();
this.play();

you set the current source of the audio element.

If you want to load a new page instead of your current page you can use :

window.location = "your_url";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM