-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerApi.ts
179 lines (137 loc) · 4.02 KB
/
PlayerApi.ts
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
import React from 'react';
import { PlayerApiImpl } from './PlayerApiImpl';
import { PlayerConsole } from './PlayerConsole';
export type PlayerType =
| 'Audio'
| 'Niconico'
| 'SoundCloud'
| 'Vimeo'
| 'YouTube';
export interface LoadedEvent {
id: string;
}
export interface TimeEvent {
duration: number | undefined;
percent: number | undefined;
seconds: number | undefined;
}
export interface PlayerOptions {
onError?(event: any): void;
onLoaded?(event: LoadedEvent): void;
onPlay?(): void;
onPause?(): void;
onEnded?(): void;
onTimeUpdate?(event: TimeEvent): void;
}
export interface IPlayerApi {
loadVideo(id: string): Promise<void>;
play(): Promise<void>;
pause(): Promise<void>;
setCurrentTime(seconds: number): Promise<void>;
setVolume(volume: number): Promise<void>;
setMuted(muted: boolean): Promise<void>;
getDuration(): Promise<number | undefined>;
getCurrentTime(): Promise<number | undefined>;
}
export interface Logger {
debug(message?: any, ...optionalParams: any): void;
error(message?: any, ...optionalParams: any): void;
}
export class PlayerApi<
TElement extends HTMLElement,
TPlayer extends PlayerApiImpl<TElement>,
> implements IPlayerApi, Logger
{
private static nextId = 1;
private readonly id: number;
private impl?: TPlayer;
constructor(
private readonly type: PlayerType,
private readonly playerElementRef: React.MutableRefObject<TElement>,
private readonly options: PlayerOptions | undefined,
private readonly loadScript: (() => Promise<void>) | undefined,
private readonly playerApiFactory: new (
logger: Logger,
playerElementRef: React.MutableRefObject<TElement>,
options: PlayerOptions | undefined,
) => TPlayer,
) {
this.id = PlayerApi.nextId++;
}
private createMessage = (message: any): string => {
return `${this.type}#${this.id} ${message}`;
};
public debug = (message?: any, ...optionalParams: any): void => {
PlayerConsole.debug(this.createMessage(message), ...optionalParams);
};
public error = (message?: any, ...optionalParams: any): void => {
PlayerConsole.error(this.createMessage(message), ...optionalParams);
};
attach = async (id: string): Promise<void> => {
this.debug('attach', id);
if (this.impl) {
this.debug('player is already attached');
return;
}
await this.loadScript?.();
this.debug('Attaching player...');
this.impl = new this.playerApiFactory(
this,
this.playerElementRef,
this.options,
);
await this.impl.attach(id);
this.debug('player attached');
};
private assertPlayerAttached = (): void => {
PlayerConsole.assert(!!this.impl, 'player is not attached');
};
detach = async (): Promise<void> => {
this.debug('detach');
this.assertPlayerAttached();
await this.impl?.detach();
this.impl = undefined;
};
loadVideo = async (id: string): Promise<void> => {
this.debug('loadVideo', id);
this.assertPlayerAttached();
this.debug('Loading video...');
await this.impl?.loadVideo(id);
this.debug('video loaded', id);
};
play = async (): Promise<void> => {
this.debug('play');
this.assertPlayerAttached();
await this.impl?.play();
};
pause = async (): Promise<void> => {
this.debug('pause');
this.assertPlayerAttached();
await this.impl?.pause();
};
setCurrentTime = async (seconds: number): Promise<void> => {
this.debug('setCurrentTime', seconds);
this.assertPlayerAttached();
await this.impl?.setCurrentTime(seconds);
};
setVolume = async (volume: number): Promise<void> => {
this.debug('setVolume', volume);
this.assertPlayerAttached();
await this.impl?.setVolume(volume);
};
setMuted = async (muted: boolean): Promise<void> => {
this.debug('setMuted', muted);
this.assertPlayerAttached();
await this.impl?.setMuted(muted);
};
getDuration = async (): Promise<number | undefined> => {
this.debug('getDuration');
this.assertPlayerAttached();
return await this.impl?.getDuration();
};
getCurrentTime = async (): Promise<number | undefined> => {
this.debug('getCurrentTime');
this.assertPlayerAttached();
return await this.impl?.getCurrentTime();
};
}