JavaFX 音频似乎没有播放 [英] JavaFX audio doesn't seem to be playing

查看:125
本文介绍了JavaFX 音频似乎没有播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaFX 的新手,最近想用 MP3 文件而不是 WAV 播放音频.据我所知,我正在做正确的事情,我没有收到任何错误,但我也听不到任何声音.

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();

检查音乐方法:

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

我也只尝试过 musicPlayer.play();.

编辑

是的,我确信 if 语句中的代码可以运行,我已经用 println 进行了检查,然后它们会打印出来.music 布尔值只是程序/游戏中设置的控制器.

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.

推荐答案

代替

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

试试这个:

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

这篇关于JavaFX 音频似乎没有播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆