HTML audio Element how to replay?

前端 未结 8 1737
自闭症患者
自闭症患者 2020-12-16 22:52

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

相关标签:
8条回答
  • 2020-12-16 22:57

    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

    0 讨论(0)
  • 2020-12-16 23:02

    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();
    
    0 讨论(0)
  • 2020-12-16 23:02

    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

    0 讨论(0)
  • 2020-12-16 23:05

    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()
    
    0 讨论(0)
  • 2020-12-16 23:06

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

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

    0 讨论(0)
  • 2020-12-16 23:06

    Use:

      audio.pause() // important!!!
      audio.currentTime = 0
      audio.play()
    
    0 讨论(0)
提交回复
热议问题