![](/img/trans.png)
[英]javafx error in MediaPlayer constructor IllegalStateException
[英]JavaFX8 MediaPlayer no sound error/wrong URL
我正在使用JavaFX 8应用程序,正在尝试使用MediaPlayer打开MP3文件。 当我启动代码的这一部分时,我传递了错误的URL错误,并且现在也没有异常,但是应用程序打开并且没有声音。 尝试过一些oracle教程,当我输入这样的URL时:“ http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv ”一切正常,因此我猜它仍然是错误的URL,但是应用程序正在启动而且我一点也不知道到底怎么了。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
//Add a scene
Group root = new Group();
Scene scene = new Scene(root, 500, 200);
File file = new File("C:\\Users\\Me\\Desktop\\SomeFile.mp3");
Media media = new Media(file.toURI().toASCIIString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// create mediaView and add media player to the viewer
MediaView mediaView = new MediaView(mediaPlayer);
((Group)scene.getRoot()).getChildren().add(mediaView);
//show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
考虑到文件已正确定位并加载,可以执行以下操作:
file.toURI().toURL().toExternalForm()
如果您正在编写一个播放各种媒体文件的应用程序,请考虑使用用户界面来获取外部文件资源的路径,而不是对其进行硬编码。 您可以使用FileChooser或DirectoryChooser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.