Control multiple videos with one control ngx-plyr

Viewed 432

I want to control multiple videos with one custom control, i've created minimal repo here https://stackblitz.com/edit/ngx-plyr-cxla4q?file=src%2Fapp%2Fapp.component.html i've tried using querySelectorAll then listen to an event but not working. did anyone know how to do it?

EDIT

I have found the solutions, here's what i got

i change plyr tag to

<video controls crossorigin playsinline
  data-poster="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.jpg" class="js-player">
  <!-- Video files -->
  <source src="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4" type="video/mp4"
    size="576" />
  <source src="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-720p.mp4" type="video/mp4"
    size="720" />
  <source src="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-1080p.mp4" type="video/mp4"
    size="1080" />
</video>

then i initiate the player using this code

this.listPlayer = Plyr.setup('.js-player', { controls });

and then in play function i have this

  play(): void {
    this.listPlayer.map(
      e => {
        e.play();
        e.config.muted = true;
        e.config.volume = 0;
      }
    )
  }

which solution is the best practices if u compare @yogen darji solution to mine?

1 Answers

There are multiple mistakes,

  1. Use ViewChildren to query all your PlyrComponent

    @ViewChildren(PlyrComponent) plyr: QueryList<PlyrComponent>;

  2. Loop through list of components and play each

    this.plyr.toArray().forEach(plyr => plyr.player.play());

Sample code with HTML and TS

<plyr class="player"  plyrTitle="Video 1" [plyrSources]="videoSources"
 (plyrInit)="player = $event" (plyrPlay)="played($event)"></plyr>
<plyr class="player" appMakered plyrTitle="Video 2" [plyrSources]="videoSources2"
  (plyrInit)="player = $event" (plyrPlay)="played($event)"></plyr>

@ViewChildren(PlyrComponent) plyr: QueryList<PlyrComponent>;
play(): void {
    this.plyr.toArray().forEach(plyr => plyr.player.play());
    console.log(this.elements);        
}

stop(): void {
    this.plyr.toArray().forEach(plyr => plyr.player.stop());        
}

Check this updated stackblitz

Related