Js控制HTML5 Audio 音乐播放和关闭

时间:2024-03-11 11:49:34

参考W3C详细文档:http://www.w3school.com.cn/jsref/dom_obj_audio.asp

pause()方法只是暂停,这里重新load之后,再次play,音乐可重头播放

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>test</title>
    <script language="javascript" type="text/javascript">
        function autoPlay() {
            var myAuto = document.getElementById(\'myaudio\');
            myAuto.play();
        }
        function closePlay() {
            var myAuto = document.getElementById(\'myaudio\');
            myAuto.pause();
            myAuto.load();
        }
    </script>
</head>

<body>
    <audio src="111.mp3" id="myaudio" controls="controls" loop="false" hidden="true">

    </audio>
    <input type="button" onclick="autoPlay()" value="播放" />
    <input type="button" onclick="closePlay()" value="关闭" />

</body>

</html>