Skip to content

Instantly share code, notes, and snippets.

@tommydunn
Created July 9, 2025 08:37
Show Gist options
  • Select an option

  • Save tommydunn/906c82d6fa61e0651a4d99123fc998c6 to your computer and use it in GitHub Desktop.

Select an option

Save tommydunn/906c82d6fa61e0651a4d99123fc998c6 to your computer and use it in GitHub Desktop.
To check if an HTML5 video element is "ready" in an Angular 18 application, you can leverage the events and properties of the native HTMLMediaElement.

is "ready"

To check if an HTML5 video element is "ready" in an Angular 18 application, you can leverage the events and properties of the native HTMLMediaElement.

1. Using canplaythrough event:

The canplaythrough event fires when the user agent estimates that it can play the media to the end without stopping for further buffering. This is typically the most reliable indicator that the video is truly ready for smooth playback. TypeScript

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-video-player',
  template: `
    <video #myVideo src="path/to/your/video.mp4" controls>
</video>
    <div *ngIf="!videoReady">Loading video...</div>
  `,
})
export class VideoPlayerComponent implements AfterViewInit {
  @ViewChild('myVideo') videoElementRef!: ElementRef<HTMLVideoElement>;
  videoReady: boolean = false;

  ngAfterViewInit(): void {
    const videoElement = this.videoElementRef.nativeElement;

    videoElement.addEventListener('canplaythrough', () => {
      this.videoReady = true;
      console.log('Video is ready to play through!');
      // You can now safely perform actions like playing the video
      // videoElement.play();
    });

    videoElement.addEventListener('error', (event) => {
      console.error('Video error:', event);
      // Handle video loading errors
    });
  }
}

2. Using readyState property:

The readyState property of the video element indicates the readiness state of the media. You can check if it's equal to or greater than HTMLMediaElement.HAVE_ENOUGH_DATA (or 4), which signifies that enough data is available to play the media to the end without interruption. TypeScript

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-video-player',
  template: `
    <video #myVideo src="path/to/your/video.mp4" controls>
</video>
    <div *ngIf="!videoReady">Loading video...</div>
  `,
})
export class VideoPlayerComponent implements AfterViewInit {
  @ViewChild('myVideo') videoElementRef!: ElementRef<HTMLVideoElement>;
  videoReady: boolean = false;

  ngAfterViewInit(): void {
    const videoElement = this.videoElementRef.nativeElement;

    // Check initial readyState in case video loads quickly
    if (videoElement.readyState >= HTMLMediaElement.HAVE_ENOUGH_DATA) {
      this.videoReady = true;
      console.log('Video is already ready!');
    } else {
      videoElement.addEventListener('canplaythrough', () => {
        this.videoReady = true;
        console.log('Video is ready to play through!');
      });
    }

    videoElement.addEventListener('error', (event) => {
      console.error('Video error:', event);
    });
  }
}

Considerations:

loadedmetadata vs. canplay vs. canplaythrough:

  • loadedmetadata: Fired when the video's metadata (duration, dimensions, etc.) is loaded, but not necessarily enough data to play.
  • canplay: Fired when the video can start playing, but might need to pause for buffering later.
  • canplaythrough: The most robust indicator for smooth playback without buffering interruptions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment