音频播放器兼容


<script>
// 检查是否正在播放
var spd = '${spd!""}';
var pit = '${pit!""}';
var vol = '${vol!""}';
var per = '${per!""}';
var myToken = '${myToken!""}';
var cuid = '${cuid!""}';
function play(text) {
if (text.indexOf("流程流程") > -1) {
text = text.replace("流程流程", "流程")
}
var url = "http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid="+cuid+"&tok="+myToken+"&tex="+text+"&vol="+vol+"&per="+per+"&spd="+spd+"&pit=" + pit;
setUrl(url);
}
urls=new Array()
if(navigator.appName == "Microsoft Internet Explorer"&&IEVersion()<11) {
//embed不能判断是否暂停的状态
setInterval('playSound()', 5000);
}else{
setInterval('playSound()', 1000);
}
function setUrl(url){
urls.push(url);
}
//递归播放新音频,前一个音频播放完成才会播放新的
function audioIsPlay(strAudio) {
var audioPlayer = document.querySelector('audio#media');
if (audioPlayer.paused) {
// 暂停中
$("#media").remove();
$("body").append(strAudio);
var audio = document.getElementById( "media" );
//浏览器支持 audion
audio.play();
} else {
// 播放中
window.setTimeout("audioIsPlay(\""+strAudio+"\")",2000);
}
}

function playSound(){
if(urls.length<1){
return;
}
//IE内核浏览器
var url=encodeURI(encodeURI(urls.pop()));
if(navigator.appName == "Microsoft Internet Explorer"&&IEVersion()<11){
var strEmbed = '<embed id="media" name="embedPlay" src="'+url+'" autostart="true" hidden="true" loop="false"></embed>';
if ( $("#media").length < 1 ){
$( "body" ).append( strEmbed );
}else{
$("#media").remove();
$( "body" ).append( strEmbed );
}
var embed = document.embedPlay;

//浏览器不支持 audion,则使用 embed 播放
embed.volume = 100;
//embed.play();这个不需要
}else{
//非IE内核浏览器
var strAudio = "<audio id='media' src='"+url+"' hidden='true'>";
if ( $("#media").length < 1 ){
$( "body" ).append(strAudio);
var audio = document.getElementById( "media" );
//浏览器支持 audion
audio.play();
}else{
audioIsPlay(strAudio);
}
}
}
</script>

 

posted @ 2018-03-05 14:27  kasher  阅读(229)  评论(0编辑  收藏  举报