Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 46c1bea

Browse files
authored
feat: LSDV-5033: Add the option to use WebAudioPlayer for Audio (#1360)
* feat: LSDV-5033: Add the option to use WebAudioPlayer for Audio * fix the audio buffer * fix play source for webaudio player * formatting of audio model file * putting back the doc param for player * feedback changes * fix player volume and playback rate setting along with webaudio waveform init --------- Co-authored-by: bmartel <[email protected]>
1 parent 6436e4e commit 46c1bea

File tree

13 files changed

+553
-338
lines changed

13 files changed

+553
-338
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"printWidth": 120,
33
"trailingComma": "all",
4-
"singleQuote": false,
4+
"singleQuote": true,
55
"jsxBracketSameLine": false,
66
"bracketSpacing": true,
77
"semi": true
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { WaveformAudio } from '../Media/WaveformAudio';
2+
import { Player } from './Player';
3+
4+
export class Html5Player extends Player {
5+
mute() {
6+
super.mute();
7+
if (this.audio?.el) {
8+
this.audio.el.muted = true;
9+
}
10+
}
11+
12+
unmute() {
13+
super.unmute();
14+
if (this.audio?.el) {
15+
this.audio.el.muted = false;
16+
}
17+
}
18+
19+
/**
20+
* Get current playback speed
21+
*/
22+
get rate() {
23+
if (this.audio?.el) {
24+
if (this.audio.el.playbackRate !== this._rate) {
25+
this.audio.el.playbackRate = this._rate; // restore the correct rate
26+
}
27+
}
28+
29+
return this._rate;
30+
}
31+
32+
/**
33+
* Set playback speed
34+
*/
35+
set rate(value: number) {
36+
const rateChanged = this._rate !== value;
37+
38+
this._rate = value;
39+
40+
if (rateChanged) {
41+
if (this.audio?.el) {
42+
this.audio.el.playbackRate = value;
43+
}
44+
this.wf.invoke('rateChanged', [value]);
45+
}
46+
}
47+
48+
init(audio: WaveformAudio) {
49+
super.init(audio);
50+
51+
if (!this.audio || !this.audio.el) return;
52+
53+
this.audio.on('resetSource', this.handleResetSource);
54+
55+
this.audio.el.addEventListener('play', this.handlePlayed);
56+
this.audio.el.addEventListener('pause', this.handlePaused);
57+
}
58+
59+
destroy() {
60+
super.destroy();
61+
62+
if (this.audio?.el) {
63+
this.audio.el.removeEventListener('play', this.handlePlayed);
64+
this.audio.el.removeEventListener('pause', this.handlePaused);
65+
}
66+
}
67+
68+
protected adjustVolume(): void {
69+
if (this.audio?.el) {
70+
this.audio.el.volume = this.volume;
71+
}
72+
}
73+
74+
protected playAudio(_start?: number, _duration?: number): void {
75+
if (!this.audio || !this.audio.el) return;
76+
77+
this.audio.el.currentTime = this.currentTime;
78+
this.audio.el.addEventListener('ended', this.handleEnded);
79+
this.bufferPromise = new Promise(resolve => {
80+
this.bufferResolve = resolve;
81+
});
82+
83+
const time = this.currentTime;
84+
85+
Promise.all([
86+
this.audio.el.play(),
87+
this.bufferPromise,
88+
])
89+
.then(() => {
90+
this.timestamp = performance.now();
91+
92+
// We need to compensate for the time it took to load the buffer
93+
// otherwise the audio will be out of sync of the timer we use to
94+
// render updates
95+
if (this.audio?.el) {
96+
// This must not be notifying of this adjustment otherwise it can cause sync issues and near infinite loops
97+
this.setCurrentTime(time);
98+
this.audio.el.currentTime = this.currentTime;
99+
this.watch();
100+
}
101+
});
102+
}
103+
104+
protected updateCurrentSourceTime(timeChanged: boolean) {
105+
if (timeChanged && this.audio?.el) {
106+
this.audio.el.currentTime = this.time;
107+
}
108+
}
109+
110+
protected canPause() {
111+
return !!(this.audio?.el && !this.audio.el.paused && this.hasPlayed);
112+
}
113+
114+
protected disconnectSource(): boolean {
115+
if (super.disconnectSource()) {
116+
this.audio?.el?.removeEventListener('ended', this.handleEnded);
117+
return true;
118+
}
119+
return false;
120+
}
121+
122+
protected handleResetSource = async () => {
123+
if (!this.audio?.el) return;
124+
125+
const wasPlaying = this.playing;
126+
127+
this.stop();
128+
this.audio.el.load();
129+
130+
if (wasPlaying) this.play();
131+
};
132+
}

0 commit comments

Comments
 (0)