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);
        });
}
相关文章
相关标签/搜索