This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch feature/audio-player into main (#93)
Implement Audio Player - Add valueChangeEvent to Setting class - Implement Audio Player - Add AudioType enum - Make AudioPlayer available through Services
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import EngineEvent from "../event/engineevent.js"; | ||
import Setting from "../settings/setting.js"; | ||
import SettingsManager from "../settings/settingsmanager.js"; | ||
import SimpleSetting from "../settings/simplesetting.js"; | ||
import AudioType from "./audiotype.js"; | ||
|
||
class AudioPlayer { | ||
private _settingsManager: SettingsManager; | ||
|
||
private _currentMusic: HTMLAudioElement | null; | ||
private _musicEnabled: Setting<boolean>; | ||
private _soundEnabled: Setting<boolean>; | ||
|
||
public get currentMusic(): HTMLAudioElement | null { | ||
return this._currentMusic; | ||
} | ||
|
||
public constructor(settingsManager: SettingsManager) { | ||
this._settingsManager = settingsManager; | ||
this._currentMusic = null; | ||
|
||
this._musicEnabled = this._settingsManager.getOrSet( | ||
"audio.music", | ||
new SimpleSetting<boolean>(true) | ||
); | ||
|
||
this._soundEnabled = this._settingsManager.getOrSet( | ||
"audio.sound", | ||
new SimpleSetting<boolean>(true) | ||
); | ||
|
||
this._musicEnabled.valueChangeEvent.subscribe( | ||
this.onMusicSettingChanges.bind(this) | ||
); | ||
} | ||
|
||
public playMusic() { | ||
this._currentMusic?.play(); | ||
} | ||
|
||
public pauseMusic() { | ||
this._currentMusic?.pause(); | ||
} | ||
|
||
public play(audioType: AudioType, audio: HTMLAudioElement) { | ||
if (audioType === AudioType.Music) { | ||
if (!this._musicEnabled.get()) { | ||
return; | ||
} | ||
|
||
this.pauseMusic(); | ||
audio.loop = true; | ||
this._currentMusic = audio; | ||
} else if (audioType === AudioType.Sound) { | ||
if (!this._soundEnabled.get()) { | ||
return; | ||
} | ||
} | ||
|
||
audio.pause(); | ||
audio.currentTime = 0; | ||
audio.play(); | ||
} | ||
|
||
private onMusicSettingChanges(valueChangeEvent: EngineEvent<boolean>) { | ||
if (valueChangeEvent.eventData) { | ||
this.playMusic(); | ||
} else { | ||
this.pauseMusic(); | ||
} | ||
} | ||
} | ||
|
||
export default AudioPlayer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enum AudioType { | ||
Music, | ||
Sound, | ||
} | ||
|
||
export default AudioType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters