javascript - mediaElement.play() 方法返回一个被拒绝的 promise

javascript - mediaElement.play() 方法返回一个被拒绝的 promise

标签 javascript html html5-video

我无法在移动浏览器上播放我的视频。使用 Safari 远程调试时,我遇到以下问题:
Unhandled Promise Rejection: AbortError: The operation was aborted.

我找到了这个解决方案:https://developers.google.com/web/updates/2017/06/play-request-was-interrupted

但我不知道,如何在我的代码中使用它来解决问题。

html 复制代码
<video muted id="video" class="absolute right-0 bottom-0 min-w-full min-h-full w-auto" controls>
    <source src="/path/to/video.mp4" type="video/mp4">
</video>
let video = document.getElementbyId('video');
let video_cta = document.getElementbyId('video_cta');

//this doesn't fix my problem
var videoPromise = video.play();

if (videoPromise != undefined) {
    videoPromise.then(function(_) {
      video.pause();
      video.currentTime = 0;
  });
}

//start video after click the button
video_cta.addEventListener('click' function() {
    video.play()
})

最佳答案

首先是 autoplay属性是必需的。

复制代码
<video src='...' controls muted`**autoplay**`></video>`
`<video src='...' controls muted` 自动播放 `></video>

videoPromise是对视频的引用并调用 .play()方法,它将在 Promise 中工作。

复制代码
const videoPromise = document.querySelector('video').play();

OP 代码中也存在依赖关系 .getElementById()被误用:

复制代码
let video = document.getElement`**b**`yId('video');` `let video = document.getElement` 湾 `yId('video');`
`let video_cta = document.getElement`**b**`yId('video_cta');` `let video_cta = document.getElement` 湾 `yId('video_cta');

Google article OP 中提供的还提到 <source>标签将无法处理 reject适本地:

Use

复制代码
<video src='...'></video>

Not

复制代码
<video> <source src='...'> </video>

这应该停止错误消息:

复制代码
Unhandled Promise Rejection: AbortError: The operation was aborted.

演示 1 使用 Promise和方法.then() . 演示 2 使用 async/await . async function被包裹在一个 IIFE ( I mmediately I nvoked F unction Expression )

演示 1

promise 承诺

js 复制代码
let cta = document.querySelector('#cta');

const videoPromise = document.querySelector('video').play();

if (videoPromise != undefined) {
  videoPromise.then(_ => {
video.play();
  });
}

cta.addEventListener('click', function() {
  video.play();
});
<video src="https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005609.mp4" id="video" class="absolute right-0 bottom-0 min-w-full min-h-full w-auto" controls autoplay muted></video>
<button id='cta'>CTA</button>

演示 2

async/await

js 复制代码
let cta = document.querySelector('#cta');

const video = document.querySelector('video');

(function() {
  playMedia();
})();

async function playMedia() {
  try {
    await video.play();
  } catch (err) {}
}

cta.addEventListener('click', function() {
  video.play();
});
<video src="https://storage04.dropshots.com/photos6000/photos/1381926/20170326/005609.mp4" id="video" class="absolute right-0 bottom-0 min-w-full min-h-full w-auto" controls autoplay muted></video>
<button id='cta'>CTA</button>

关于javascript - mediaElement.play() 方法返回一个被拒绝的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55953639/

相关推荐
程序猿的程1 天前
开源一个 React 股票 K 线图组件,传个股票代码就能画图
前端·javascript
大雨还洅下1 天前
前端JS: 虚拟dom是什么? 原理? 优缺点?
javascript
唐叔在学习1 天前
[前端特效] 左滑显示按钮的实现介绍
前端·javascript
青青家的小灰灰1 天前
深入理解事件循环:异步编程的基石
前端·javascript·面试
前端Hardy1 天前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy1 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
UIUV1 天前
node:child_process spawn 模块学习笔记
javascript·后端·node.js
烛阴1 天前
Three.js 零基础入门:手把手打造交互式 3D 几何体展示系统
javascript·webgl·three.js
颜酱1 天前
单调栈:从模板到实战
javascript·后端·算法
_AaronWong1 天前
Electron 实现仿豆包划词取词功能:从 AI 生成到落地踩坑记
前端·javascript·vue.js