JavaFX audio doesn't seem to be playing

删除回忆录丶 提交于 2019-12-11 04:35:39

问题


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:


instead of

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

try this:

final Media media = new Media(sound.toString());


来源:https://stackoverflow.com/questions/38606289/javafx-audio-doesnt-seem-to-be-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!