Display progress of . video playing using Iframe youtube player api

user2995358

I've embedded youtube video using their ipframe API. I am able to load a video and play. However, I want to show the current time of the video on the UI. It never refreshes until there is UI event, like I click on a button. I am using Angular 8. here is my template code:

...
<button class="btn btn-dark mb-5" (click)="onMute()">Mute</button>
<button class="btn btn-dark mb-5" (click)="onUnmute()">Unmute</button>
</div>
<p>Progress: {{videoProg}}</p>

<!-- YT Player will embed IP frame below.-->
<div class="mt-1" id="player"></div>

Here is my component code:

  this.subscription = source.subscribe(val => {
  this.videoProg = this.cleanTime()
  console.log("check progress fired. ", this.videoProg);
});

Here is how the UI looks: Progress vlue stays empty until some UI event like button click happens.

Poul Kruijt

Just like I expected :) your events come from a non so called monkey patched event system within the youtube library. Any events fired from here are not patched by the zone.js and therefore will run outside of the angular NgZone.

To fix this, you need to inject the NgZone and use the ngZone.run() to get your code back into the change detection:

constructor(private ngZone: NgZone) {}

events: {
  'onStateChange': (event) => this.ngZone.run(() => this.onPlayerStateChange(event)),
  'onError': (event) => this.ngZone.run(() => this.onPlayerError(event)),
  'onReady': (event) => this.ngZone.run(() => this.onPlayerReady(event))
}

working stack

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iframe Youtube video showing black screen using DialogFragment and only audio is playing in Webview

YouTube IFrame Player shows 'This Video is Unavailable' when play button is pressed

How to force high quality thumbnails for YouTube Player API iframe Embeds?

Loading Youtube iFrame API video at a lower quality

How do I change the playback speed of the currently playing YouTube video, with JavaScript and the HTML5 player?

getPlaylist returns an array with only one video when using the Youtube IFrame API

Youtube Player video not loading

ios 10 youtube iframe not playing

YouTube IFrame Player API getVideoData is removed: how to get title?

Youtube iframe API events not triggering when player is added at later stage

Video is not auto playing on IOS by using react-player npm

Add video to page using YouTube Player API

Is there a way to enable controls in video player playing youtube videos flutter web

Youtube Player View not displaying video using with youtube API and channel import

How to listen to time change events with the YouTube IFrame Player API?

Loading the Youtube Player API inside an iframe

Youtube Player Iframe, sound with no video on firefox

Limit video buffer to X seconds in YouTube iframe player

Youtube Player API Difference (Chromeless vs IFrame) - Which is better?

Getting "Time Elapsed" of a playing video from the YouTube API in ActionScript 3.0

Custom progress bar using YouTube API

Video player: HTML5 vs Youtube API

Adding a video to a playlist using the Youtube Player IFrame API

YouTube IFrame, Angular2, Routing, video starts playing on switch

youtube player api - playlist of video not created

YouTube iFrame Player API failed to execute postMessage on DOMWindow

YouTube Player API hide play button, icons, & video title in iframe

Youtube iframe video using php

Youtube iframe player blackspace above and below video