1. 程式人生 > >利用JavaScript實現音訊檔案的播放和暫停

利用JavaScript實現音訊檔案的播放和暫停

HTML5 規定了一種通過 audio 元素來包含音訊的標準方法。
audio 元素能夠播放聲音檔案或者音訊流。
注意看,a.paused表示當前音訊的狀態,而音訊的暫停和播放對應的方法分別為pause()和play() ——-(自認為很值得注意的地方 我當時就犯了這種錯誤)

<button onclick="clickA(this)">播放/暫停</button>
<audio id="audio" src="raw/1.mp3">您的瀏覽器不支援</audio>


<script>
    var a = document.getElementById("audio"
); function clickA() { //如果暫停 點選即可播放 if (a.paused) { // 播放 a.play(); //如果播放 點選暫停 } else if (a.play()) { //暫停 a.pause(); } }
</script>

謝謝!!!