Spaces:
Running
Running
| export class AudioPlayer { | |
| constructor(audioContext, url) { | |
| this.audioContext = audioContext; | |
| this.audio = new Audio(); | |
| this.audio.crossOrigin = 'anonymous'; | |
| this.audio.src = url; | |
| this.gainNode = this.audioContext.createGain(); | |
| this.analyser = this.audioContext.createAnalyser(); | |
| this.source = this.audioContext.createMediaElementSource(this.audio); | |
| this.source.connect(this.analyser); | |
| this.analyser.connect(this.gainNode); | |
| this.gainNode.connect(this.audioContext.destination); | |
| this.isPlaying = false; | |
| // Set fixed volume | |
| this.gainNode.gain.value = 0.5; | |
| } | |
| play() { | |
| this.audioContext.resume(); | |
| this.audio.play(); | |
| this.isPlaying = true; | |
| } | |
| pause() { | |
| this.audio.pause(); | |
| this.isPlaying = false; | |
| } | |
| connectAnalyser(analyser) { | |
| this.source.disconnect(this.analyser); | |
| this.source.connect(analyser); | |
| analyser.connect(this.gainNode); | |
| } | |
| } |