JavaFX8 MediaPlayer没有声音错误/URL错误 [英] JavaFX8 MediaPlayer no sound error/wrong URL

查看:78
本文介绍了JavaFX8 MediaPlayer没有声音错误/URL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaFX 8应用,并且正在尝试使用MediaPlayer打开MP3文件.当我启动这段代码时,我传递了错误的URL错误,并且现在也没有异常,但是应用程序打开并且没有声音.尝试过一些oracle教程,当我输入这样的URL时:" http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv 一切正常,因此我猜它仍然是错误的URL,但是应用程序正在启动,我一点也不知道到底是哪里出了问题.

I'm working on JavaFX 8 app and I'm trying to open MP3 file using MediaPlayer. I had passed wrong URL errors and I've no exceptions right now, when I start such part of code, but app opens and there is no sound. Tried with some oracle tutorial and when i put such URL: "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv" everything is working so I guess it's still wrong URL, but app is starting and I've litterally no clue whats wrong.

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

推荐答案

考虑正确定位并加载文件,您可以执行以下操作:

Considering the file is correctly located and loaded you can do the following:

file.toURI().toURL().toExternalForm()

如果您正在编写一个播放各种媒体文件的应用程序,请考虑使用用户界面来获取外部文件资源的路径,而不是对其进行硬编码.您可以使用 FileChooser DirectoryChooser

If you are writing an app that plays various media files, consider using user interface to obtain paths to external file resources instead of hardcoding them. You can use FileChooser or DirectoryChooser

这篇关于JavaFX8 MediaPlayer没有声音错误/URL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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