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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | import * as dom from '../dom/dom';
import {Defer} from '../func/defer';
import * as func from '../func/func';
import {Raf} from '../raf/raf';
export interface VideoPlayOptions {
// Whether you prefer the video to loop or not.
loop: boolean;
// Optional raf callback you can hook into.
rafCallback: Function;
}
/**
* Utility class to handle playing / stopping / playing segments of
* a given video element.
*
* ```ts
* let video = document.getElementById('video');
* let videoPlayer = new VideoPlayer(video, {
* rafCallback: ()=> {
* console.log(videoPlayer.getTime());
* }
* });
*
* videoPlayer.load().then(()=> {
* console.log("loaded");
* })
*
*
* videoPlayer.setPointer(1.2);
* videoPlayer.play();
* videoPlayer.pause();
* videoPlayer.playToAndStop(4);
* videoPlayer.playFrom(5,6);
*
* ```
*/
export class VideoPlayer {
/**
* video: HTMLVideoElement;
*/
private video: HTMLVideoElement;
private videoReady: Defer;
private raf: Raf;
private settingsData: VideoPlayOptions;
/**
* A stop queue.
*/
private stopQueue: number | null = null;
constructor(video: HTMLVideoElement, options: VideoPlayOptions) {
this.video = video;
if (!this.video) {
throw new Error('Oops, video is not found');
}
this.raf = new Raf(this.onRaf.bind(this));
this.raf
.runWhenElementIsInview(this.video, {
rootMargin: '500px 0px 500px 0px',
})
.then(() => {
this.raf.start();
});
// Ensure we don't run into autoplay issues.
this.video.muted = true;
this.video.playsInline = true;
this.video.autoplay = false;
this.settingsData = {
...{
loop: false,
rafCallback: null,
},
...(options || {}),
};
this.videoReady = new Defer();
}
/**
* Loads the video competely first.
* TODO (uxder): Look for a better way to check completely readyState of video
* besides polling.
*/
public 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();
}
/**
* Tests whether the video is currently playing.
*/
public isPlaying() {
return dom.testVideoIsPlaying(this.video);
}
/**
* Pauses a video. Does not reset the pointer.
*
* ```
* // Pause where it at.
* videoPlayer.pause();
*
* // Pauses and resets pointer to 0 mark.
* videoPlayer.pause();
* videoPlayer.reset();
* ```
*/
public pause() {
this.video.pause();
}
/**
* Resets the video to the start.
*/
public reset() {
this.video.currentTime = 0;
}
/**
* Plays a video from the current pointer.
*/
public play(): void {
if (!this.isPlaying()) {
const playPromise = this.video.play();
playPromise.then(() => {}).catch(() => {});
}
}
/**
* Sets the video currentTime to a specific position.
* @param seconds
*/
public setPointer(seconds: number) {
this.video.currentTime = seconds;
}
/**
* Starts playing from the current pointer and stops at a specific time.
* If the current time
*/
public playToAndStop(seconds: number) {
this.stopQueue = seconds;
this.play();
}
/**
* Plays a specific segment of the video.
* @param seconds
*/
public playFromTo(fromSeconds: number, toSeconds: number) {
this.setPointer(fromSeconds);
this.playToAndStop(toSeconds);
}
/**
* Gets the video element.
*/
public getVideo(): HTMLVideoElement {
return this.video;
}
/**
* Gets the current time.
*/
public getTime(): number {
return this.video.currentTime;
}
/**
* Internal clock to check status of video. We could use
* timeupdate event on the video but it seems to be slow.
*/
private onRaf() {
this.settingsData.rafCallback && this.settingsData.rafCallback();
if (this.stopQueue && this.getTime() >= this.stopQueue) {
this.pause();
this.stopQueue = null;
}
}
/**
* Disposes the class.
*/
public dispose() {
this.raf.dispose();
}
}
|