JS对不同的浏览器播放音频文件

网上找了好多方法,都没有成功,js如果想要完全控制object对象,会提示TypeError: player.controls is undefined错误,是因为需要安装音频播放器插件。无意中灵机一动采用了一种便捷的方法轻松实现了,无需安装插件(ie自带的media player插件),感谢灵感。

Code:

<head>
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache, must-revalidate"/>
    <meta http-equiv="expires" content="0"/>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
</head>
<div id="audioDiv" style="display: none;"></div>
<a href="javascript:void(0);" onclick="startSound();">播放</a>
<a href="javascript:void(0);" onclick="stopSound();">停止</a>
<script type="text/javascript">
    function startSound() {
        var mp3snd = "文件路径";
        var myaudiohtml = "";
        if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
            myaudiohtml += '<embed src="' + mp3snd + '" type=application/x-mplayer2 width="0" height="0" autostart="1"/>';
        } else if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) {
            myaudiohtml += '<object data="' + mp3snd + '" type="application/x-mplayer2" width="0" height="0">';
            myaudiohtml += '<param name="filename" value="' + mp3snd + '">';
            myaudiohtml += '<param name="autostart" value="1">';
            myaudiohtml += '<param name="playcount" value="infinite">';
            myaudiohtml += '</object>';
        } else {
            myaudiohtml += '<audio src="' + mp3snd + '" autoplay="autoplay" loop="loop">';
            myaudiohtml += '<object data="' + mp3snd + '" type="application/x-mplayer2" width="0" height="0">';
            myaudiohtml += '<param name="filename" value="' + mp3snd + '">';
            myaudiohtml += '<param name="autostart" value="1">';
            myaudiohtml += '<embed height="2" width="2" src="' + mp3snd + '" pluginspage="http://www.apple.com/quicktime/download/" type="video/quicktime" controller="false" controls="false" autoplay="true" autostart="true" loop="false"><br>';
            myaudiohtml += '</embed></object>';
            myaudiohtml += '</audio>';
        }
        document.getElementById("audioDiv").innerHTML = myaudiohtml;
    }

    function stopSound() {
        document.getElementById("audioDiv").innerHTML = "";
    }
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值