HTML5 Audio not working in ie7 or ie8

后端 未结 3 1915
攒了一身酷
攒了一身酷 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:

    
    
    

    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!

提交回复
热议问题