-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from einaskoi/einar/Audio
add working audio system and weekend and work themes
- Loading branch information
Showing
14 changed files
with
97 additions
and
41 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Audio/Audio.java
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,24 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Audio; | ||
|
|
||
| public enum Audio { | ||
|
|
||
| // Sound effects | ||
| OPEN_APP("/Audio/open_app.wav"), | ||
| CLOSE_APP("/Audio/close_app.wav"), | ||
| CLOCK("/Audio/clock.wav"), | ||
| TRANSACTION("/Audio/transaction.wav"), | ||
|
|
||
| // Background music | ||
| WORK_THEME("/Audio/work_theme.mp3"), | ||
| WEEKEND_THEME("/Audio/weekend_theme.mp3"); | ||
|
|
||
| private String path; | ||
|
|
||
| Audio(String path) { | ||
| this.path = path; | ||
| } | ||
|
|
||
| public String getPath() { | ||
| return path; | ||
| } | ||
| } |
61 changes: 41 additions & 20 deletions
61
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Audio/AudioManager.java
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 |
|---|---|---|
| @@ -1,39 +1,60 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Audio; | ||
|
|
||
| import javafx.scene.media.AudioClip; | ||
| import javafx.scene.media.Media; | ||
| import javafx.scene.media.MediaPlayer; | ||
|
|
||
| import java.util.EnumMap; | ||
| import java.util.Map; | ||
|
|
||
| public class AudioManager { | ||
|
|
||
| private static AudioManager instance = new AudioManager(); | ||
| private Map<SoundEffect, AudioClip> sounds = new EnumMap<>(SoundEffect.class); | ||
| private double volume = 1.0; | ||
| private static final AudioManager instance = new AudioManager(); | ||
| private final Map<Audio, String> urlCache = new EnumMap<>(Audio.class); | ||
| private MediaPlayer bgPlayer; | ||
|
|
||
| public AudioManager() { | ||
| private AudioManager() { | ||
| for (Audio audio : Audio.values()) { | ||
| var url = getClass().getResource(audio.getPath()); | ||
| urlCache.put(audio, url.toExternalForm()); | ||
| } | ||
| } | ||
|
|
||
| for (SoundEffect effect : SoundEffect.values()) { | ||
| public static AudioManager getInstance() { return instance; } | ||
|
|
||
| AudioClip clip = new AudioClip( | ||
| getClass() | ||
| .getResource(effect.getPath()) | ||
| .toExternalForm() | ||
| ); | ||
| public void playSFX(Audio audio) { | ||
| String url = urlCache.get(audio); | ||
|
|
||
| sounds.put(effect, clip); | ||
| if (url == null) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| public static AudioManager getInstance() { | ||
| return instance; | ||
| MediaPlayer sfxPlayer = new MediaPlayer(new Media(url)); | ||
| sfxPlayer.setOnEndOfMedia(() -> { | ||
| sfxPlayer.stop(); | ||
| sfxPlayer.dispose(); | ||
| }); | ||
|
|
||
| sfxPlayer.play(); | ||
| } | ||
|
|
||
| public void play(SoundEffect sound) { | ||
| AudioClip audioClip = sounds.get(sound); | ||
| public void playBgMusic(Audio audio) { | ||
| stopBgMusic(); | ||
|
|
||
| String url = urlCache.get(audio); | ||
| if (url == null) { | ||
| return; | ||
| } | ||
|
|
||
| bgPlayer = new MediaPlayer(new Media(url)); | ||
| bgPlayer.setVolume(1); | ||
| bgPlayer.play(); | ||
| } | ||
|
|
||
| if (audioClip != null) { | ||
| audioClip.play(volume); | ||
| public void stopBgMusic() { | ||
| if (bgPlayer != null) { | ||
| bgPlayer.stop(); | ||
| bgPlayer.dispose(); | ||
| bgPlayer = null; | ||
| } | ||
| } | ||
| } | ||
| } |
17 changes: 0 additions & 17 deletions
17
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Audio/SoundEffect.java
This file was deleted.
Oops, something went wrong.
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
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.