forked from katspaugh/wavesurfer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
131 lines (131 loc) · 3.75 KB
/
player.js
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
import EventEmitter from './event-emitter.js';
class Player extends EventEmitter {
constructor(options) {
super();
this.isExternalMedia = false;
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.onceMediaEvent('canplay', () => {
if (options.playbackRate != null) {
this.media.playbackRate = options.playbackRate;
}
});
}
}
onMediaEvent(event, callback, options) {
this.media.addEventListener(event, callback, options);
return () => this.media.removeEventListener(event, callback);
}
onceMediaEvent(event, callback) {
return this.onMediaEvent(event, callback, { once: true });
}
getSrc() {
return this.media.currentSrc || this.media.src || '';
}
revokeSrc() {
const src = this.getSrc();
if (src.startsWith('blob:')) {
URL.revokeObjectURL(src);
}
}
setSrc(url, blob) {
const src = this.getSrc();
if (src === url)
return;
this.revokeSrc();
const newSrc = blob instanceof Blob ? URL.createObjectURL(blob) : url;
this.media.src = newSrc;
this.media.load();
}
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();
}
setMediaElement(element) {
this.media = element;
}
/** Start playing the audio */
play() {
return this.media.play();
}
/** Pause the audio */
pause() {
this.media.pause();
}
/** Check if the audio is playing */
isPlaying() {
return !this.media.paused && !this.media.ended;
}
/** Jumpt to a specific time in the audio (in seconds) */
setTime(time) {
this.media.currentTime = time;
}
/** Get the duration of the audio in seconds */
getDuration() {
return this.media.duration;
}
/** Get the current audio position in seconds */
getCurrentTime() {
return this.media.currentTime;
}
/** Get the audio volume */
getVolume() {
return this.media.volume;
}
/** Set the audio volume */
setVolume(volume) {
this.media.volume = volume;
}
/** Get the audio muted state */
getMuted() {
return this.media.muted;
}
/** Mute or unmute the audio */
setMuted(muted) {
this.media.muted = muted;
}
/** Get the playback speed */
getPlaybackRate() {
return this.media.playbackRate;
}
/** Set the playback speed, pass an optional false to NOT preserve the pitch */
setPlaybackRate(rate, preservePitch) {
// preservePitch is true by default in most browsers
if (preservePitch != null) {
this.media.preservesPitch = preservePitch;
}
this.media.playbackRate = rate;
}
/** Get the HTML media element */
getMediaElement() {
return this.media;
}
/** Set a sink id to change the audio output device */
setSinkId(sinkId) {
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId
const media = this.media;
return media.setSinkId(sinkId);
}
}
export default Player;