aboutsummaryrefslogtreecommitdiff
path: root/src/main/SoundController.java
blob: 55d40f45e847ce565aa6b8d7b1e6467dc29c6e7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main;

import java.io.File;
import java.util.HashMap;

import gui.Driver;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class SoundController 
{
    public MediaPlayer songMediaPlayer;
    public MediaPlayer sfxMediaPlayer;
    private HashMap<String,File> presets = new HashMap<>();
    private File mainMenuSong = new File("src/assets/MenuMusicPlaceholder.wav");

    public SoundController() 
    {
        presets.put("forward", new File("src/assets/MenuForward.wav"));
        presets.put("backward", new File("src/assets/MenuBackward.wav"));
        presets.put("hit", new File("src/assets/Hitsound.wav"));
        presets.put("miss", new File("src/assets/Miss.wav"));
        playMenuSong();
    }

    public void playSong(File songFile)
    {
        if (songMediaPlayer != null)
        {
            songMediaPlayer.stop();
        }
        Media song = new Media(songFile.toURI().toString());
        songMediaPlayer = new MediaPlayer(song);
        songMediaPlayer.volumeProperty().bind(Driver.settingsController.musicVol);
        songMediaPlayer.play();
    }

    private void playMenuSong()
    {
        playSong(mainMenuSong);
        songMediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
        songMediaPlayer.play();
    }

    public void endSong()
    {
        playMenuSong();
    }

    public void playSfx(File sfxFile) 
    {
        if (sfxMediaPlayer != null)
        {
            sfxMediaPlayer.stop();
        }
        Media sound = new Media(sfxFile.toURI().toString());
        sfxMediaPlayer = new MediaPlayer(sound);
        sfxMediaPlayer.volumeProperty().bind(Driver.settingsController.effectsVol); //not working yet
        sfxMediaPlayer.play();
    }

    public void playSfx(String preset)
    {
        playSfx(presets.get(preset));
    }
}