在浏览器中播放音频时出错
Error while playing Audio in browser
我正在使用javascript代码以用户指定的开始时间播放音频
var audio_player = document.getElementById("audi");
//'audi' is ID of audio tag
audio_player.currentTime = audi_start_time;
//Start time from where audio should start to play
audio_player.play();
当我运行网站时,我可以播放"audi_start_time"中指定的时间的音频,但大多数时候我收到错误
'试图使用不可用或不再可用的
对象 [中断此错误]
audio_player.currentTime = audi_start_time;
任何帮助将不胜感激。 提前谢谢
我认为您正在尝试在文档准备就绪之前设置currentTime
,
因为当您在 window.onlaoad 中设置 currentTime 时,它正在工作。
这是您要执行的操作-> JSFiddle demo-1(不起作用)
这是你应该做的 -> JS小提琴演示-2(工作)
编辑:
好的,我知道了。如果您尝试设置动态附加的音频/视频标签currentTime
,则应等待音频标签准备好播放。
因此,您可以使用oncanplay事件来检测它,
例如
<div id="content"></div>
<button onclick="play()">play</button>
<script type="text/javascript">
function play() {
var audio = document.createElement("audio");
audio.controls = "controls";
audio.innerHTML = '<source src="http://www.w3schools.com/html5/song.ogg" type="audio/ogg" />'+
'<source src="http://www.w3schools.com/html5/song.mp3" type="audio/mp3" />';
var content = document.getElementById("content");
content.innerHTML = "";
content.appendChild(audio);
audio.addEventListener("canplay", function() {
this.currentTime = 5;
},true);
audio.play();
}
</script>
这是一个有效的jsFiddle演示
在设置当前时间之前播放音频。我整晚都在桌子上敲打我的头,然后我发现了这个线程:https://github.com/johndyer/mediaelement/issues/243。这最终对我有用:
audioplayer.src = source;
audioplayer.play();
audioplayer.addEventListener('playing', function () {
if (loadPlayed == false)
{
audioplayer.currentTime = Time;
audioplayer.play();
loadPlayed = true;
}
else {
audioplayer.removeEventListener('playing', this);
}
});
相关文章:
- 如何播放部分音频文件
- 音频控件在mouseover上显示,在mouseout上淡出
- HTML5音频加载和播放获胜'我不能在iPad上工作
- javascript中的数组出错
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 是否可以在浏览器中使用纯JavaScript保存音频流
- 在Meteor项目中安装并包含npm模块后出错
- 解析JSON并从中删除对象会出错
- AngularJS指令出错-无法读取属性'编译'的未定义
- angular不会更改json的音频源
- 通过幻灯片更改事件停止使用jquery的音频
- 使用javascript在MVC中查找网格长度时出错
- 尝试调用AngularJS上的函数时出错
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- 全局安装gull后出错
- 音频和动画在javasctipt循环中同步
- HTML5 音频出错
- 在浏览器中播放音频时出错
- "获取音频时出错“;使用Recorder.js录制音频
- Jquery可点击音频滑块,在窗口宽度变化时出错