如何正确使用 JavaFX MediaPlayer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22490064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use JavaFX MediaPlayer correctly?
提问by Dima Maligin
I'm writing a simple game and trying to play sounds but I can't get it to work when I create the Media object it throws IllegalArgumentException
. I'm not much of a Java coder and any help will be appreciated.
Here is a sample code:
我正在编写一个简单的游戏并尝试播放声音,但是当我创建它抛出的 Media 对象时我无法让它工作IllegalArgumentException
。我不是一个 Java 编码员,任何帮助将不胜感激。这是一个示例代码:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Main{
public static void main(String[] args) {
Media pick = new Media("put.mp3"); //throws here
MediaPlayer player = new MediaPlayer(pick);
player.play();
}
}
Obviously "put.mp3" exists and located in the correct directory, I checked the path using: System.out.println(System.getProperty("user.dir"));
显然“put.mp3”存在并位于正确的目录中,我使用以下方法检查了路径: System.out.println(System.getProperty("user.dir"));
what am I doing wrong here?
我在这里做错了什么?
采纳答案by ItachiUchiha
The problem is because you are trying to run JavaFX scene graph control outside of JavaFX Application thread
.
问题是因为您试图在JavaFX Application thread
.
Run all JavaFX scene graph nodes inside the JavaFX application thread.
在 JavaFX 应用程序线程内运行所有 JavaFX 场景图节点。
You can start a JavaFX thread by extending JavaFX Application
class and overriding the start()
method.
您可以通过扩展 JavaFXApplication
类并覆盖该start()
方法来启动 JavaFX 线程。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Media pick = new Media("put.mp3"); // replace this with your own audio file
MediaPlayer player = new MediaPlayer(pick);
// Add a mediaView, to display the media. Its necessary !
// This mediaView is added to a Pane
MediaView mediaView = new MediaView(player);
// Add to scene
Group root = new Group(mediaView);
Scene scene = new Scene(root, 500, 200);
// Show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
// Play the media once the stage is shown
player.play();
}
public static void main(String[] args) {
launch(args);
}
}
回答by Dima Maligin
Ok thanks to @ItachiUchiha insight on the matter I was able to solve my problem, It seems that any code that uses javaFX must run from within javaFX application Thread but not every program has to use javaFX API. In short what I did is start my game from within the Application.start(Stage ps)
like so:
好的,感谢@ItachiUchiha 对我能够解决我的问题的洞察力,似乎任何使用 javaFX 的代码都必须从 javaFX 应用程序线程中运行,但并非每个程序都必须使用 javaFX API。简而言之,我所做的是从这样的内部开始我的游戏Application.start(Stage ps)
:
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
new Game(9,9,BasicRobot.FACING.SOUTH, 19);
}
public static void main(String[] args) throws InterruptedException {
launch();
}
}
That way the Game class and everything it creates and uses can use javaFX. To play the sounds I created a Utils class:
这样 Game 类及其创建和使用的所有内容都可以使用 javaFX。为了播放声音,我创建了一个 Utils 类:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Utils {
public static void playSound(String fileName){
Media m = new Media("file:///" + System.getProperty("user.dir").replace('\', '/') + "/" + fileName);
MediaPlayer player = new MediaPlayer(m);
player.play();
}
}
and now all I have to do to play a sound is call Utils.playSound("fileName.mp3")
from anywhere inside my Game.
现在播放声音所需要做的就是Utils.playSound("fileName.mp3")
从我的游戏中的任何地方调用。