[英]video.play() get Uncaught (in promise) DOMException
I spend half-day trying to figure why video.play() is not working on Chrome. 我花了半天的时间来弄清楚video.play()为什么不能在Chrome上运行。 I got this error :
Uncaught (in promise) DOMException
which seems to be really frequent. 我收到此错误:
Uncaught (in promise) DOMException
似乎真的很频繁。 I read a bunch of stuffs but either I did not understand how to integrate the solution or it didn't work. 我读了很多东西,但要么我不明白如何集成该解决方案,要么它不起作用。
Here is my code : 这是我的代码:
$(document).ready(function() {
// Get media - with autoplay disabled (audio or video)
var media = $('.playOnScroll').not("[autoplay='autoplay']");
var tolerancePixel = 50;
//document.querySelector(".playOnScroll").load();
var playPromise = document.querySelector('video').play();
console.log(playPromise);
function checkMedia(){
// Get current browser top and bottom
var scrollTop = $(window).scrollTop() + tolerancePixel;
var scrollBottom = $(window).scrollTop() + $(window).height() - tolerancePixel;
media.each(function(index, el) {
var yTopMedia = $(this).offset().top;
var yBottomMedia = $(this).height() + yTopMedia;
if(scrollTop < yBottomMedia && scrollBottom > yTopMedia){
$(this).get(0).pause();
$(this).get(0).play();
} else {
$(this).get(0).pause();
}
});
}
$(document).on('scroll', checkMedia);
});
I found this but I don't know how to integrate it and I don't even know if it's working. 我找到了它,但是我不知道如何集成它,甚至不知道它是否有效。 May it can help you :
可以帮助您:
var playPromise = document.querySelector('video').play();
// In browsers that don’t yet support this functionality,
// playPromise won’t be defined.
if (playPromise !== undefined) {
playPromise.then(function() {
// Automatic playback started!
}).catch(function(error) {
// Automatic playback failed.
// Show a UI element to let the user manually start playback.
});
}
My code is working perfectly on others browsers but I can't make it work on Chrome. 我的代码可以在其他浏览器上完美运行,但无法在Chrome上运行。 Please rescue me ahah.
请救救我啊。
I find an alternative solution which is not what I wanted, but it works, it could still help someone so there it is : 我找到了不是我想要的替代解决方案,但是可行,仍然可以帮助某人,所以有:
$(document).ready(function() {
// Get media - with autoplay disabled (audio or video)
var media = $('.playOnScroll').not("[autoplay='autoplay']");
var tolerancePixel = 50;
var video = document.getElementsByClassName("playOnScroll");
video[0].load();
function checkMedia(){
// Get current browser top and bottom
var scrollTop = $(window).scrollTop() + tolerancePixel;
var scrollBottom = $(window).scrollTop() + $(window).height() - tolerancePixel;
media.each(function(index, el) {
var yTopMedia = $(this).offset().top;
var yBottomMedia = $(this).height() + yTopMedia;
if(scrollTop < yBottomMedia && scrollBottom > yTopMedia){
playVideo();
} else {
$(this).get(0).pause();
}
});
}
async function playVideo() {
try {
$('.playOnScroll')[0].play();
} catch(err) {
video[0].setAttribute("controls","controls")
}
}
$(document).on('scroll', checkMedia);
});
I just use the try and catch to allow user to control the video play manually if it's not working automatically. 我只是使用try and catch来允许用户手动控制视频播放(如果无法自动运行)。 Not perfect at all...
一点都不完美...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.