利用html5標籤audio在不同用戶端下適配播放音頻

來源:互聯網
上載者:User

標籤:javascript html5 web前端 移動開發 audio

//聲明:ios系統不支援falsh,故而採用各平台支援的html5 audio標籤

<script>

//建立audio標籤

        var audio = document.createElement("audio");

//增加audio 屬性id

        audio.id = "audio";

//設定播放檔案路徑

        audio.src = "sound/music.mp3";

//設定是否迴圈播放

        audio.loop = "loop";

//擷取當前用戶端環境資訊

        var ua = window.navigator.userAgent.toLowerCase();

//判斷當前環境並做出相應處理

        if(ua.match(/iphone/i) == "iphone" || ua.match(/ipod/i) == "ipod" || ua.match(/ipad/i) == "ipad"){

    //當前是ios相關環境,為canvas節點添加事件touchstart監聽

            document.getElementById("canvas").addEventListener("touchstart", function(){

//音頻播放

                audio.play();

            });

        }else if(ua.match(/android/i) == "android"){

            //對android系統不做處理

        }else{

    //對於非ios、android系統,則採用監聽click事件即可

            document.getElementById("canvas").addEventListener("click", function(){

//音頻播放

                audio.play();

            });

        }

    </script>


本文出自 “mirwangsir” 部落格,請務必保留此出處http://mirwangsir.blog.51cto.com/7723278/1565734

利用html5標籤audio在不同用戶端下適配播放音頻

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.