-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
player.ts
175 lines (146 loc) · 4.25 KB
/
player.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
import EventEmitter, { type GeneralEventTypes } from './event-emitter.js'
type PlayerOptions = {
media?: HTMLMediaElement
mediaControls?: boolean
autoplay?: boolean
playbackRate?: number
}
class Player<T extends GeneralEventTypes> extends EventEmitter<T> {
protected media: HTMLMediaElement
private isExternalMedia = false
constructor(options: PlayerOptions) {
super()
if (options.media) {
this.media = options.media
this.isExternalMedia = true
} else {
this.media = document.createElement('audio')
}
// Controls
if (options.mediaControls) {
this.media.controls = true
}
// Autoplay
if (options.autoplay) {
this.media.autoplay = true
}
// Speed
if (options.playbackRate != null) {
this.onMediaEvent(
'canplay',
() => {
if (options.playbackRate != null) {
this.media.playbackRate = options.playbackRate
}
},
{ once: true },
)
}
}
protected onMediaEvent<K extends keyof HTMLElementEventMap>(
event: K,
callback: (ev: HTMLElementEventMap[K]) => void,
options?: boolean | AddEventListenerOptions,
): () => void {
this.media.addEventListener(event, callback, options)
return () => this.media.removeEventListener(event, callback, options)
}
protected getSrc() {
return this.media.currentSrc || this.media.src || ''
}
private revokeSrc() {
const src = this.getSrc()
if (src.startsWith('blob:')) {
URL.revokeObjectURL(src)
}
}
private canPlayType(type: string): boolean {
return this.media.canPlayType(type) !== ''
}
protected setSrc(url: string, blob?: Blob) {
const src = this.getSrc()
if (src === url) return
this.revokeSrc()
const newSrc = blob instanceof Blob && this.canPlayType(blob.type) ? URL.createObjectURL(blob) : url
this.media.src = newSrc
}
protected destroy() {
this.media.pause()
if (this.isExternalMedia) return
this.media.remove()
this.revokeSrc()
this.media.src = ''
// Load resets the media element to its initial state
this.media.load()
}
protected setMediaElement(element: HTMLMediaElement) {
this.media = element
}
/** Start playing the audio */
public async play(): Promise<void> {
return this.media.play()
}
/** Pause the audio */
public pause(): void {
this.media.pause()
}
/** Check if the audio is playing */
public isPlaying(): boolean {
return !this.media.paused && !this.media.ended
}
/** Jump to a specific time in the audio (in seconds) */
public setTime(time: number) {
this.media.currentTime = time
}
/** Get the duration of the audio in seconds */
public getDuration(): number {
return this.media.duration
}
/** Get the current audio position in seconds */
public getCurrentTime(): number {
return this.media.currentTime
}
/** Get the audio volume */
public getVolume(): number {
return this.media.volume
}
/** Set the audio volume */
public setVolume(volume: number) {
this.media.volume = volume
}
/** Get the audio muted state */
public getMuted(): boolean {
return this.media.muted
}
/** Mute or unmute the audio */
public setMuted(muted: boolean) {
this.media.muted = muted
}
/** Get the playback speed */
public getPlaybackRate(): number {
return this.media.playbackRate
}
/** Check if the audio is seeking */
public isSeeking(): boolean {
return this.media.seeking
}
/** Set the playback speed, pass an optional false to NOT preserve the pitch */
public setPlaybackRate(rate: number, preservePitch?: boolean) {
// preservePitch is true by default in most browsers
if (preservePitch != null) {
this.media.preservesPitch = preservePitch
}
this.media.playbackRate = rate
}
/** Get the HTML media element */
public getMediaElement(): HTMLMediaElement {
return this.media
}
/** Set a sink id to change the audio output device */
public setSinkId(sinkId: string): Promise<void> {
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId
const media = this.media as HTMLAudioElement & { setSinkId: (sinkId: string) => Promise<void> }
return media.setSinkId(sinkId)
}
}
export default Player