[feature] add volume control

This commit is contained in:
MathieuG-P
2023-03-03 01:46:28 +01:00
parent c4319f1c6c
commit b16f7150e8
3 changed files with 37 additions and 3 deletions
@@ -1,5 +1,6 @@
import { Observable } from "rxjs";
import { BehaviorSubject } from "rxjs";
import { ConfigurationService } from "./configuration.service";
export class AudioPlayerService{
@@ -10,18 +11,31 @@ export class AudioPlayerService{
return AudioPlayerService.instance;
}
private readonly config: ConfigurationService;
private readonly player: HTMLAudioElement;
private readonly _src$: BehaviorSubject<string> = new BehaviorSubject("");
private readonly _playing$: BehaviorSubject<boolean> = new BehaviorSubject(false);
private readonly _bpm$: BehaviorSubject<number> = new BehaviorSubject(0);
private readonly _volume$: BehaviorSubject<number> = new BehaviorSubject(0);
private constructor(){
this.config = ConfigurationService.getInstance();
this.player = new Audio();
this.player.onplay = () => this._playing$.next(true);
this.player.onpause = () => this._playing$.next(false);
this.player.onended = () => this._playing$.next(false);
this.player.onvolumechange = () => {
this._volume$.next(this.player.volume);
this.config.set("audio-level", this.player.volume, true);
}
this.player.volume = this.config.get("audio-level") ?? 0.5;
}
public play(src: string, bpm = 0): Promise<void>{
@@ -41,6 +55,10 @@ export class AudioPlayerService{
return this.player.play();
}
public setVolume(volume: number): void{
this.player.volume = volume;
}
public get src$(): Observable<string>{
return this._src$.asObservable();
}
@@ -50,9 +68,13 @@ export class AudioPlayerService{
public get bpm$(): Observable<number>{
return this._bpm$.asObservable();
}
public get volume$(): Observable<number>{
return this._volume$.asObservable();
}
public get src(): string{ return this._src$.value; }
public get playing(): boolean{ return this._playing$.value; }
public get bpm(): number{ return this._bpm$.value; }
public get volume(): number{ return this._volume$.value; }
}