开发者

HTML audio Element how to replay?

I se开发者_C百科tup 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?


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();


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


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


Use:

  audio.pause() // important!!!
  audio.currentTime = 0
  audio.play()


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

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


don't use node.load() this is way too slow for slow devices.

To stop, you should just use:

node.currentTime = 0

To replay you can do:

node.currentTime = 0
node.play()


You can try this:

<audio id="audiotag1" src="audio/flute_c_long_01.wav" preload="auto"></audio>
<a href="javascript:play_single_sound();">Play 5-sec sound on single channel</a>
<script type="text/javascript">
    function play_single_sound() {
        document.getElementById('audiotag1').play();
    }
</script>

Ref: http://www.storiesinflight.com/html5/audio.html


In HTML5 you can just set audioElement.loop='true';

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜