Javascript HTML5 音频在 ie7 或 ie8 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13827637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 14:51:28  来源:igfitidea点击:

HTML5 Audio not working in ie7 or ie8

javascripthtmlinternet-explorer

提问by Nick Rivers

When testing in IE7/8 my script crashes and I get this error...

在 IE7/8 中进行测试时,我的脚本崩溃了,并且出现此错误...

SCRIPT438: Object doesn't support property or method 'play'

SCRIPT438:对象不支持属性或方法“播放”

I'm using the HTML5 audio tag to embed and play audio on my webpage.

我正在使用 HTML5 音频标签在我的网页上嵌入和播放音频。

<div id="auido-container">
        <audio id="music" loop="loop">
            <source src="audio/holiday-for-mr-anderson-60secs.mp3"></source>
            <source src="audio/holiday-for-mr-anderson-60secs.ogg"></source>
            Your browser isn't invited for super fun audio time.
        </audio>
        <audio id="sound">
            <source src="audio/pop.mp3"></source>
            <source src="audio/pop.ogg"></source>
            Your browser isn't invited for super fun audio time.
        </audio>

    </div>

My JS looks like this:

我的 JS 看起来像这样:

start.click(function(){
    audio.play();
});

I've included this in my header:

我已将其包含在我的标题中:

<!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

Does anyone know of any solutions or fixes?

有谁知道任何解决方案或修复程序?

回答by Eric J.

Many older browsers including IE7/8 do not (fully) support HTML 5.

包括 IE7/8 在内的许多旧浏览器不(完全)支持 HTML 5。

You can use Modernizrto detect whether the current browser supports audio.

您可以使用Modernizr来检测当前浏览器是否支持音频。

There are polyfillsfor many features including audio that can add support for missing features

有许多功能的polyfill,包括可以添加对缺失功能的支持的音频

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills

(scroll down to the Audio section for options)

(向下滚动到“音频”部分以获取选项)

The polyfills work on a wide range of supported browsers and browser versions.

polyfills 适用于各种受支持的浏览器和浏览器版本。

回答by Sree

I had a similar problem and here is how I fixed it (this now works in IE8 and plays wav files too!). The trick is to use audio tag for HTML5 compatible browsers and embed tag for older IE ones.

我有一个类似的问题,这是我修复它的方法(这现在可以在 IE8 中运行并且也可以播放 wav 文件!)。诀窍是为 HTML5 兼容浏览器使用音频标签,为旧版 IE 浏览器使用嵌入标签。

Create two elements in your HTML page:

在 HTML 页面中创建两个元素:

<audio id="audiotag" src="images/beep-03.wav" preload="auto"></audio>
<embed id="audiotagII" src='images/beep-03.wav' autostart='false' loop='false' width='2' height='0'></embed>

following is the JavaScript part of playing sound in older as well as newer browsers:

以下是在较旧和较新的浏览器中播放声音的 JavaScript 部分:

//Returns the version of Windows Internet Explorer or a -1. Available on the Internet!
function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.);
    }
    return rv;
}

var browserVer = getInternetExplorerVersion();
if (browserVer > -1 && browserVer < 9) {
    document.getElementById('audiotagII').play();   
} else {
    document.getElementById('audiotag').play();
}

Thank you!

谢谢!

回答by Franz Payer

If the browser does not support HTML5 audio, there is not much you can do besides use a substitute for those browsers. According to w3schools:

如果浏览器不支持 HTML5 音频,除了使用这些浏览器的替代品之外,您无能为力。根据 w3schools 的说法:

Internet Explorer 8 and earlier versions, do not support the element.

Internet Explorer 8 及更早版本,不支持该元素。

Source: http://www.w3schools.com/html/html5_audio.asp

来源:http: //www.w3schools.com/html/html5_audio.asp