简体   繁体   English

JavaFX无法播放mp3文件

[英]JavaFX can't play mp3 files

I followed this tutorial: http://what-when-how.com/javafx-2/playing-audio-using-the-media-classes-javafx-2-part-1/ 我遵循了本教程: http : //what-when-how.com/javafx-2/playing-audio-using-the-media-classes-javafx-2-part-1/

package audioVideo;

import java.net.URL;

import javafx.application.Application;
import javafx.scene.media.*;
import javafx.stage.*;

public class AudioPlayer1 extends Application
{
    public static void main(String args[])
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        URL resource = getClass().getResource("resources/sample.mp3");
        Media media = new Media(resource.toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();

        primaryStage.setTitle("Audio Player 1");
        primaryStage.setWidth(200);
        primaryStage.setHeight(200);
        primaryStage.show();
    }
}

And I have the appropriate resources folder in the same directory as the respective .class file, but I still can't play the audio. 而且我在与相应.class文件位于同一目录中的适当资源文件夹,但仍无法播放音频。 The mp3 file isn't the same one in the tutorial, but I also have a .wav equivalent and found that JavaFX can play *.wav files out of the box but not mp3 files. mp3文件不是本教程中的文件,但我也有一个.wav等效文件,发现JavaFX可以直接播放* .wav文件,但不能播放mp3文件。 What's going on? 这是怎么回事?

I'm using Ubuntu 15.04 64-bit via Eclipse. 我正在通过Eclipse使用Ubuntu 15.04 64位。 It can't be a resource issue because the .wav file is in the same folder as the .mp3 one, but the former can be played yet the latter can't. 这不是资源问题,因为.wav文件与.mp3文件位于同一文件夹中,但是前者可以播放,而后者不能播放。 Do I need certain dependencies in order for the mp3 file to be played correctly? 我是否需要某些依赖项才能正确播放mp3文件? I can play the mp3 files from Ubuntu directly. 我可以直接从Ubuntu播放mp3文件。

First, import the following: 首先,导入以下内容:

import java.nio.file.Paths;

Then, when you create your media, do the following: 然后,在创建媒体时,请执行以下操作:

Media media = new Media(("yourAudio.fileExtension").toUri().toString());
MediaPlayer player = new MediaPlayer(media);

That should do the trick (for a mp3 at least). 那应该可以解决问题(至少要播放mp3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM