最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 Audio not working in ie7 or ie8 - Stack Overflow

programmeradmin1浏览0评论

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

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

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

<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:

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

I've included this in my header:

<!--[if IE]>
        <script src=".js"></script>
    <![endif]-->

Does anyone know of any solutions or fixes?

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

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

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

<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:

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?

Share Improve this question asked Dec 11, 2012 at 20:05 Nick RiversNick Rivers 2941 gold badge6 silver badges18 bronze badges 4
  • Support for audio elements was introduced in IE9. – nnnnnn Commented Dec 11, 2012 at 20:07
  • 4 The HTML5 Shiv only allows styling of HTML5 elements; it does not enable HTML5 functionality. – apsillers Commented Dec 11, 2012 at 20:08
  • "HTML5Shiv is a JavaScript workaround, discovered by Sjoerd Visscher, to enable styling of HTML5 elements in versions of Internet Explorer prior to version 9, which do not allow unknown elements to be styled without JavaScript" – Ian Commented Dec 11, 2012 at 20:08
  • 1 Its not supported in IE7 or IE8. You'll need to use an alternative player like flash – Jules Commented Dec 11, 2012 at 20:10
Add a comment  | 

3 Answers 3

Reset to default 11

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

You can use Modernizr to detect whether the current browser supports audio.

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

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.

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.

Create two elements in your HTML page:

<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:

//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.$1);
    }
    return rv;
}

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

Thank you!

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:

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

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

发布评论

评论列表(0)

  1. 暂无评论