|
| 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