1. 程式人生 > >HTML5做的音訊播放器,可以動態的更換音訊檔案的內容

HTML5做的音訊播放器,可以動態的更換音訊檔案的內容

在做一個播放按鈕時突然用到了這個技術,具體還是蠻簡單的,一看就會,就是那個音訊檔案的src屬性動態賦值的時候花了點時間。看程式碼吧!

function playOrPaused(obj) {
            
            var url = "f:/lrh.mp3";
            //alert(url);
            var audio = document.getElementById("audio");
            if (audio) {

            }
            else {
                var board = document.getElementById("_button");
                var a2 = document.createElement("audio");
                a2.setAttribute("src", url);
                a2.setAttribute("controls", "controls");
                a2.setAttribute("id", "audio");
                audio = board.appendChild(a2);
            }
            if (audio.paused) {
                audio.play();
                document.getElementById("btn").value = "暫停";
                return;
            } else {
                audio.pause();
                document.getElementById("btn").value = "播放";
            }
        }

<body>
    <form id="form1" runat="server">
        <div>
            <div id="_button" runat="server" style="width: 100%; height: 40px;">


                <input id="btn" type="button" value="播放" style="height: 30px; width: 80px; margin-top: 2px" onclick="playOrPaused(this);" />


            </div>
            </div>
    </form>
</body>