All files / src/dom video-progress.ts

0% Statements 0/18
0% Branches 0/6
0% Functions 0/6
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
import {Defer} from '../func/defer';
import * as func from '../func/func';
import * as mathf from '../mathf/mathf';
import {Fps} from '../time/fps';
 
/**
 * VideoProgress is a class that allows you to quick seek videos.
 * Note that this is a HIGHLY experimental class and it generally only works
 * well for small size mp4 on Chrome and Safari (desktop).  It totally janks on Firefox.
 * See /examples/playground video progress for a sample.
 *
 * @experimental
 * @unstable
 * @hidden
 */
export class VideoProgress {
  private video: HTMLVideoElement;
  private videoReady: Defer;
  private fps: Fps;
 
  constructor(video: HTMLVideoElement) {
    this.video = video;
    this.fps = new Fps(30);
    // Ensure we don't run into autoplay issues.
    this.video.muted = true;
    this.video.playsInline = true;
    this.video.autoplay = false;
 
    this.videoReady = new Defer();
  }
 
  /**
   * Sets the FPS.
   */
  setFps(fps: number) {
    this.fps.setFps(fps);
  }
 
  /**
   * Loads the video competely first.
   * TODO (uxder): Look for a better way to check completely readyState of video
   *     besides polling.
   */
  load(): Promise<void> {
    this.video.load();
    if (this.video.readyState === 4) {
      this.videoReady.resolve();
    } else {
      func
        .waitUntil(() => this.video.readyState === 4)
        .then(() => {
          this.videoReady.resolve();
        });
    }
    return this.videoReady.getPromise();
  }
 
  setProgress(progress: number) {
    if (this.fps.canRun()) {
      const interpolatedTime = mathf.lerp(0, this.video.duration, progress);
      // this.video.pause();
      // if (this.video['fastSeek']) {
      // this.video['fastSeek'](interpolatedTime);
      // } else {
      if (interpolatedTime) {
        this.video.currentTime = interpolatedTime;
      }
      // }
    }
  }
}