HTML audio Element how to replay?

26,963

Solution 1

please read this

http://www.position-absolute.com/articles/introduction-to-the-html5-audio-tag-javascript-manipulation/

and for replay event

you can set option, on replay click

audioElement.currentTime=0;
audioElement.play();

Solution 2

The suggestions above did not worked for me or where not applicable. I needed to replay sound when my code needed to do it.

The following code worked (at least chrome):

audioElement.setAttribute('src', audioSrc); audioElement.play();

in other words, setting the src attribute again, then play() did the job

Solution 3

The html5 audio element has a loop attribute set it to loop=loop

http://www.w3schools.com/html5/html5_audio.asp

Solution 4

If the server hosting the video does not support seeking on client (HTTP RangeRequest), the command

audioElement.currentTime=0;

from @JapanPro's answer is silently ignored in Chromium. This behavior is filled as crbug #121765.

There is a handy guide on MDN how to configure your server to play nice. But if you cannot, I had success with doing

audioElement.load();

which unsurprisingly reloads the audio source and seeks to the start as a side effect. Not nice, but it is quite intuitive and it works.

source: http://www.whatwg.org/specs/web-apps/current-work/#loading-the-media-resource

Share:
26,963
Dreampuf
Author by

Dreampuf

A pythonista

Updated on July 05, 2022

Comments

  • Dreampuf
    Dreampuf about 2 years

    I setup a HTML5 page, that include a node. The page will to playing a music(mp3 format),but it just play sound to end, and I use javascript to control audio element,even so,I can't replay it and only STOP.

    Does HTML5 audio Element support REPLAY? And how to?

  • Dreampuf
    Dreampuf almost 13 years
    but I just want to a audio played, then it play again when wait user click
  • valkirilov
    valkirilov over 10 years
    This is working but makes a new request for the sound res every time.
  • user7610
    user7610 about 10 years
    @valkirilov But it works around code.google.com/p/chromium/issues/detail?id=121765
  • Basj
    Basj over 7 years
    It doesn't work on every browser. .load() and then .play() always works, but it might use more data...