1四、使用JS播放聲音

 //使用JS播放聲音
function playSoundsForHtml5(id){
        if(id< 0){
            return ;
        }            
        var staticUrl = id+ '.mp3';   
        var soundsObj = document.createElement('AUDIO');//建立聲音對象         
        soundsObj.setAttribute('src',staticUrl );//設置播放路徑
        soundsObj.setAttribute('autoplay', 'true');//設置自動播放
        document.body.appendChild(soundsObj);
        soundsObj.addEventListener('error', function(){
            document.body.removeChild(soundsObj);
        });
        soundsObj.addEventListener('ended', function(){
            document.body.removeChild(soundsObj);
        });
}
相關文章
相關標籤/搜索