HTML5 Audio not working in ie7 or ie8

后端 未结 3 1884
攒了一身酷
攒了一身酷 2021-01-21 07:55

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

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

I\'m

相关标签:
3条回答
  • 2021-01-21 08:01

    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!

    0 讨论(0)
  • 2021-01-21 08:08

    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.

    0 讨论(0)
  • 2021-01-21 08:17

    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 讨论(0)
提交回复
热议问题