1. 程式人生 > >Js控制HTML5 Audio 音樂播放和關閉

Js控制HTML5 Audio 音樂播放和關閉

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>