JavaFX audio doesn't seem to be playing

2019-07-24 01:57发布

I am fairly new to JavaFX and recently wanted to play audio with an MP3 file rather than WAV. From what I can tell, I am doing things correctly and I don't get any errors, but I also don't hear any sound.

I will post the parts of my code that matter below. If I'm missing something please let me know. Thanks.

try {
    URL sound = getClass().getResource("/resources/origin.mp3");
    Media hit = new Media(sound.toExternalForm());
    musicPlayer = new MediaPlayer(hit);
    musicPlayer.setVolume(1.0);
}
catch(Exception e) {
    System.out.println("whoops: " + e);
}
checkMusic();

Check Music Method:

public void checkMusic() {
    if(music)
        musicPlayer.setAutoPlay(true);
    else
        musicPlayer.stop();
}

I also tried just musicPlayer.play(); as well.

EDIT

And yes, I am sure the code within the if statement runs, I have checked it with println, and they print out. The music boolean is just a controller for settings in the program/game.

1条回答
我只想做你的唯一
2楼-- · 2019-07-24 02:24

instead of

Media hit = new Media(sound.toExternalForm());

try this:

final Media media = new Media(sound.toString());
查看更多
登录 后发表回答