Search code examples
audiojavafxmedia-playermedia

Javafx how to setup a Media playlist


Ok I'm currently doing a Trivial Pursuit game project with javafx and my group wants me to add audio the problem is I have a method

public static void playSoundEffect(Sound sfx) {

    Media media=null;
    try {
        media = new Media(GameAudio.class.getClassLoader().getResource(sfx.getSound()).toURI().toString());
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

But it has its issues because if I want to mute all the audio, only the last played sound will be muted and not the whole project's audio.

I was thinking of making 2 List of MediaPlayer(SFX and Music) that contains each audio files but I'm not sure how to set this up properly... My current try was using Enum for the const strings containing the path. Then in some class i use the method above to play the sound at a certain point. But since i always call a new instance of mediaPlayer I don't have any control on it anymore and that's why I'm so lost.


Solution

  • As @James_D supposed for the mute I will use a BooleanProperty muted and call a method mediaPlayer.muteProperty().bind(muted) on each mediaplayer created.