我正在尝试在网页中查看IP摄像头的视频流,当流可以播放时,我希望它能自动开始播放。通过计时器尝试实现,如果播放失败,则再次尝试。
然而,计时器(超时)似乎没有实现这个功能,但是如果使用按钮执行脚本,则可以。我错过了什么?
请参见下面的代码。
谢谢,
Ron
PS:我已经注释掉了setTimeout函数,以使按钮起作用。
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function playVid() {
var videoElem = document.getElementById("IPcamerastream");
var playPromise = videoElem.play();
// In browsers that don’t yet support this functionality playPromise won’t be defined.
if (playPromise !== undefined) {
playPromise.then(function() {
// Automatic playback started!
videoElem.controls = true;
}).catch(function(error) {
// Automatic playback failed.
// setTimeout(playVid, 1000);
});
}
}
//setTimeout(playVid, 1000);
</script>
<button onclick="playVid()" type="button">Play Video</button><BR>
<video id="IPcamerastream" src="http://192.168.2.8:8080" width="960" height="540"></video>
</body>
</html>