我知道有类似的主题,但要么是过时的,要么是JAVA代码的答案 .
似乎android不再允许自动播放,而是需要用户手势 . 虽然,我想确保在我停止尝试之前用完所有(合理的)选项 .
播放在iOS和桌面上完美运行 .
现在有没有办法在Android设备上进行自动播放?
视频标签
muted="true"
playsinline="true"
loop="true"
class="bg-video"
autoplay="true"
>
src="videos/low-android-480.mp4"
class="bg-video-source"
type="video/mp4" // Tried also without specifying the type
/>
哈克尝试发送用户手势 (doesn't work)
const event = new MouseEvent("touchstart", {
view: window,
bubbles: true,
cancelable: true,
});
window.addEventListener("touchstart", function videoStart() {
videoElement.play();
// remove from the window and call the function we are removing
window.removeEventListener("touchstart", videoStart);
});
...
if (videoElement.paused) {
window.dispatchEvent(event);
}
只需致电 videoElement.play(); (doesn't work)
UPDATE #1 像这样修改的hack示例dispatchEvent部分
window.setTimeout(() => {
window.dispatchEvent(event);
});
在Android Chrome浏览器上播放视频 . 但不是在Native浏览器上 . 还在寻找那个解决方案......
UPDATE #2 这是Jordi Castilla回答中提到的文章的一个有用片段 . 它让我发现实际的 videoElement.play() 承诺在Android Native浏览器上被拒绝了 .
const promise = videoElement.play();
if (promise !== undefined) {
promise.then(_ => {
// Autoplay started!
alert("started autoplay");
}).catch(error => {
// Autoplay was prevented.
alert("failed autoplay");
// Show a "Play" button so that user can start playback.
});
}