尝试使用JMF在Java中播放MP3时出现格式错误

时间:2017-05-24 11:50:32

标签: java audio mp3 jmf

当我尝试使用JMF在Java中播放MP3文件时出错。我尝试过使用WAV文件,效果很好。

这是我的代码(基于this tutorial):

import javax.media.*;
import java.net.URL;

class mp3 extends Thread {
    private URL url;
    private Player playMP3;

    public mp3(String mp3) {
        try {
            this.url = new URL(mp3);
        } catch (java.net.MalformedURLException e) {
            System.out.println(e.getMessage());
        }
    }

    public void run() {

        try {
            MediaLocator mediaLocator = new MediaLocator(url);
            playMP3 = Manager.createPlayer(mediaLocator);
        } catch (java.io.IOException | NoPlayerException e) {
            System.out.println(e.getMessage());
        }

        playMP3.addControllerListener(new ControllerListener() {
            public void controllerUpdate(ControllerEvent e) {
                if (e instanceof EndOfMediaEvent) {
                    playMP3.stop();
                    playMP3.close();
                }
            }
        });
        playMP3.realize();
        playMP3.start();
    }
}

public class PlayMP3 {
    public static void main(String[] args) {
        mp3 t = new mp3("file:///C://TestMP3Player//music.wav"); // Works well
//      mp3 t = new mp3("file:///C://TestMP3Player//music.mp3"); // Doesn't work (error below)
        t.start();
        System.out.println("Playing song...");
    }
}

错误(与this post相同):

  

播放歌曲......     无法处理格式:mpeglayer3,44100.0 Hz,16位,立体声,LittleEndian,有符号,16000.0帧速率,FrameSize = 32768位   未能实现:com.sun.media.PlaybackEngine@54932122   错误:无法实现com.sun.media.PlaybackEngine@54932122

     

处理完成,退出代码为0

我不知道我是否正确安装了JMF。我只需在IntelliJ中的项目依赖项中添加包含JAR文件的JMF-2.1.1e\lib目录,如下所示:

IntelliJ project dependancies window

了解这个错误的原因是什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

似乎MP3文件的格式是JMF无法处理的。 幸运的是,我有一个音频库,也可以播放MP3文件。

https://github.com/RalleYTN/SimpleAudio

答案 1 :(得分:0)

以下是我要播放mp3的全部内容。

public static void main(String args[]) throws NoPlayerException, CannotRealizeException, IOException {
    MediaLocator ml = new MediaLocator((new File("roar_of_future.mp3").toURL()));
    Player player = Manager.createRealizedPlayer(ml);
    player.start();
}

所以请确保

  1. mp3plugin.jar和jmf.jar在类路径中
  2. JavaSDK是Java 8(32位)或7(32位),因为JMF在Java 9及更高版本上不起作用。图书馆
相关问题