javascript - HTML5 音频 DOM 异常 11 错误,currentTime=0

标签 javascript html audio

编辑: 我在 Wordpress 方面发布了这个,但一些管理员认为它在这里,因为他们说这是一个客户端问题,但我不认为这是因为他的脚本按原样工作(在非 wordpress 站点中)但只抛出 DOM 异常在 Wordpress 网站上使用脚本且仅在某些浏览器上使用时出现 11 错误。完全相同的脚本可以在 Wordpress 下运行,但只能在 Chrome 上运行。


我采用了一个脚本,当 anchor 链接悬停时播放音频剪辑并将其变成 Wordpress 插件。

它工作正常,但只适用于 Chrome (Mac)。在 Firefox 或 Safari(当前版本)上,脚本抛出 DOM 错误。

这里是错误:

INVALID_STATE_ERR:DOM 异常 11:尝试使用不可用或不再可用的对象。

产生错误的行是:html5audio.currentTime=0

以下脚本在我在非 Wordpress 站点中遇到问题的浏览器上运行良好。 http://www.javascriptkit.com/script/script2/soundlink.shtml#current

我已尝试研究问题并使用一些修复程序,但我无法开始工作。

var html5_audiotypes={ 
    "mp3": "audio/mpeg",
    "mp4": "audio/mp4",
    "ogg": "audio/ogg",
    "wav": "audio/wav"
}

function audio(sound){
    var html5audio=document.createElement('audio')
    if (html5audio.canPlayType){ //check support for HTML5 audio
        for (var i=0; i<arguments.length; i++){
            var sourceel=document.createElement('source')
            sourceel.setAttribute('src', arguments[i])
            if (arguments[i].match(/\.(\w+)$/i))
                sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
            html5audio.appendChild(sourceel)
        }
        html5audio.load()
        html5audio.playclip=function(){
            html5audio.pause()
            html5audio.currentTime=0
            html5audio.play()
        }
        return html5audio
    }
}

最佳答案

我想对于最初的发帖人来说已经太晚了,但万一其他人来看...

我有一个类似的问题 - 在我的例子中,代码在 Chrome 上工作,但据我所知没有其他问题,但它不是 Wordpress 网站。经过一些实验,我意识到只有当 currentTime 已经是 0 时才会发生错误。所以解决方案很简单:

html5audio.pause(); 
if (html5audio.currentTime != 0) {
   html5audio.currentTime=0;
}  
html5audio.play();

希望对大家有所帮助。

关于javascript - HTML5 音频 DOM 异常 11 错误,currentTime=0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390550/

相关文章:

javascript - 如何使用 Greasemonkey 重新组织 <ul> 列表?

javascript - 使用 jQuery AJAX 检索数据而无需在 php 中重新加载页面

javascript - 如果列表/数组不为空,如何禁用 Angular 中的按钮?

css - 哪些设备/浏览器支持 <video> 元素上的 background-color 属性?

php - 在空字段上运行查询时出现错误

html - Visual Studio 中 angular 在 html 中的静态类型

c# - 在 Internet Explorer 8 和 9 中播放非 pcm (gsm) ".wav"-文件

javascript - 如何以简单的方式将像素转换为em

android - super 强大的 Android 同时播放和录制

python-2.7 - 用Python播放声音?