【问题标题】:videogular2 not playing video when loadedvideogular2 加载时不播放视频
【发布时间】:2021-03-11 20:11:14
【问题描述】:

我正在使用 videogular 在 mu 网站上制作视频播放器。 我希望视频在页面加载后开始播放,而不需要用户按下开始按钮。 到目前为止,我尝试过的任何事情都没有成功。如果我按下播放按钮,视频确实可以正常播放,但我无法让它在页面加载时运行。

HTML:

<vg-player (onPlayerReady)="onPlayerReady($event)" [style.width.px]="width" [style.height.px]="height">
<vg-overlay-play></vg-overlay-play>

<vg-controls vgFor="singleVideo">
    <vg-play-pause></vg-play-pause>
    <vg-mute></vg-mute>
    <vg-volume></vg-volume>
    <vg-fullscreen></vg-fullscreen>
</vg-controls>

<video #media
    [vgMedia]="media"
    src="{{videoUrl}}"
    id="singleVideo"
    type="video/mp4"
    preload="auto"
    crossorigin
    >
</video>
</vg-player>

打字稿

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { VgAPI } from 'videogular2/compiled/core';

@Component({
  selector: 'video-player',
  templateUrl: './video-player.component.html',
  styleUrls: ['./video-player.component.css']
})
export class VideoPlayerComponent  {

  @Input() 
  set src(src: string)
  {
    this.videoUrl = src;
    console.log("changing src: " + src);
    if (this.api != null)
    {
      this.playVideo();
    }
  }
  @Input() height: number;
  @Input() width: number;
  @Output() onFinish = new EventEmitter<any>();

  videoUrl: string;
  isPlaying: boolean;

  constructor() { }

  api: VgAPI

  onPlayerReady(api: VgAPI) {
    console.log("player ready");
    this.api = api;
    this.playVideo();

    this.api.getDefaultMedia().subscriptions.ended.subscribe(
      () => {
          this.api.getDefaultMedia().currentTime = 0;
          this.nextVideo();
      }
    );
  }

  playVideo() {
    console.log("video starting")
    this.api.currentTime = 0;
    this.api.play();
  }

  nextVideo() {
    this.onFinish.emit();
  }

}

是的,我进行了调试,并且我确信在正确设置视频网址后调用了 api.play()。

【问题讨论】:

    标签: angular videogular


    【解决方案1】:

    于是我找到了解决问题的方法,结果发现它超级简单。 只需将 autoplay="true" 添加到视频组件中即可

    <video #media
        [vgMedia]="media"
        src="{{videoUrl}}"
        id="singleVideo"
        type="video/mp4"
        preload="auto"
        autoplay="true"
        crossorigin
        >
    

    但奇怪的是,我用作参考的 videogular2 showroom rep 根本没有使用此自动播放属性,尽管每次加载屏幕时都会播放视频。

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 1970-01-01
      • 2018-11-22
      • 2017-11-02
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      相关资源
      最近更新 更多