Error: Video not autoplay when app loads for the first time in angular6

Error: Video not autoplay when app loads for the first time in angular6

我正在尝试在应用首次加载时自动播放视频。但是当应用程序首次在浏览器上打开时,视频不会自动播放。

在这里,我得到了一个 link 但没有解决方案,而且仍然没有得到答复 - Video is not playing in angular 4 but refresh page is working

视频有时加载/有时不加载(在 Chrome 开发工具中检查时)。但是,视频在这两种情况下都不是第一次播放。

这是视频 link:https://stackblitz.com/edit/angular-nezdkr?file=src/app/app.component.html

在 chrome 中首次加载应用程序时不自动播放,但在 Mozilla firefox 中自动播放。

注意:我不想显示控件。

app.component.html

<router-outlet></router-outlet>

app.routing.ts

 {
        path: '',
        redirectTo: 'videoCOmp',
        pathMatch: 'full'
    },
    {
        path: 'videoCOmp',
        component: videoCOmpComponent,
        pathMatch: 'full'
    }

当应用首次在浏览器上加载并路由到 videoCOmpComponent 时,视频不会自动播放。但是,当重新加载/刷新浏览器时,视频开始播放。

videoCOmpComponent.ts

    abc0 = true;
    def0 = true;

    abc1 = false;
    def1 = false;

videoPlay: HTMLElementVideoElement;

ngOniInit(){
// I am trying to solve this issue but getting no success:-

 if(this.abc0 && this.def0 ){
  this.videoPlay =  document.querySelector('videoContainer');
 this.videoPlay.play();
  }
}

videoCOmpComponent.html

<div id="video-container" class="video-container" *ngIf="abc0 && def0">
<video id="videoContainer" muted autoplay>
 <source src="../../../../assets/videos/meeting1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<div *ngIf="abc1 && def1" class="video-container">
<video  muted autoplay="autoplay">
<source src="../../../../assets/videos/meeting2.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>

我得到了问题的答案:-

  //autoplay muted onloadedmetadata="this.muted = true"  

<video id="abc" autoplay muted onloadedmetadata="this.muted = true" >
  <source src="https://github.com/mediaelement/mediaelement-files/blob/master/big_buck_bunny.mp4?raw=true" type="video/mp4">
  Your browser does not support the video tag.
</video>

在您的视频标签中使用它。由于在 init

上静音 属性,功能崩溃
<video width="282" height="200" loop muted autoplay #video canplay)="video.play()" (loadedmetadata)="video.muted = true">
  <source src="path-to" type="video/webm">
</video>ode here

对我来说,相同问题的解决方案是将 muted 属性更改为 [muted] = true。

(正如 Dilshan Liyanage 在 上所建议的那样)