首页 > 解决方案 > 视频无法在 chrome 中播放或自动播放,甚至静音 - 有时是,有时不是

问题描述

我找不到解决方案。很抱歉把 angular 和 html 作为标签 - 这是因为我不知道谁是有罪的。

是的,我在 Stack Overflow 和许多其他来源看到“静音”视频基本上没有限制。但在我的场景中......好吧,有时它有效,有时它不起作用......有时我刷新并且它有效。

很随意。

<video *ngIf="p.background.type==='video'" [ngStyle]="{opacity: p.background.backgroundOpacity}"
           class="backgroundElement" src="../assets/backgrounds/videos/{{p.background.file}}"
           [ngClass]="{currentVideo: i === this.pageIndex}"
           muted loop></video>

const video = this.elRef.nativeElement.querySelector('.currentVideo').play();
      if (video !== undefined) {
          video.then(_ => {
             console.log('worked at this time');
          }).catch(error => {
            console.log(error);
          });
        }
      }, 2000);

我从 Promise 得到的错误是:消息:“play() 失败,因为用户没有先与文档交互。”

但这是一个静音视频。那么,有什么建议吗?

标签: htmlangularhtml5-video

解决方案


推荐阅读