简体   繁体   English

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

[英]Format error when trying to play MP3 in Java with JMF

I have an error when I try to play an MP3 file in Java using JMF.当我尝试使用 JMF 在 Java 中播放 MP3 文件时出现错误。 I have tried with a WAV file and it works well.我尝试过使用 WAV 文件,效果很好。

Here is my code (based on 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...");
    }
}

And the error (the same as in this post ):和错误(与这篇文章相同):

Playing song... Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit, Stereo, LittleEndian, Signed, 16000.0 frame rate, FrameSize=32768 bits Failed to realize: com.sun.media.PlaybackEngine@54932122 Error: Unable to realize com.sun.media.PlaybackEngine@54932122播放歌曲...无法处理格式:mpeglayer3, 44100.0 Hz, 16-bit, Stereo, LittleEndian, Signed, 16000.0 frame rate, FrameSize=32768 bits 无法实现:com.sun.media.PlaybackEngine@54932122 错误:无法实现 com.sun.media.PlaybackEngine@54932122

Process finished with exit code 0进程以退出代码 0 结束

I do not know if I installed JMF correctly.我不知道我是否正确安装了 JMF。 I just add the JMF-2.1.1e\\lib directory that contains the JAR files in the project dependancies in IntelliJ like this:我只是在 IntelliJ 的项目依赖项中添加包含 JAR 文件的JMF-2.1.1e\\lib目录,如下所示:

IntelliJ 项目依赖窗口

An idea of what makes this mistake?知道是什么导致了这个错误?

Thank you for your help!感谢您的帮助!

Seems the MP3 file is in a format that JMF can't handle.似乎 MP3 文件的格式是 JMF 无法处理的。 Luckily for you I have an audio library which is also able to play MP3 files.幸运的是,我有一个可以播放 MP3 文件的音频库。

https://github.com/RalleYTN/SimpleAudio https://github.com/RalleYTN/SimpleAudio

The following is all I need to play mp3.以下是我播放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();
}

So please make sure所以请确保

  1. mp3plugin.jar and jmf.jar is in the classpath mp3plugin.jar和 jmf.jar 在类路径中
  2. JavaSDK is Java 8 (32bit) or 7 (32bit) because JMF is not working on Java 9 and above. JavaSDK 是 Java 8(32 位)或 7(32 位),因为 JMF 不适用于 Java 9 及更高版本。 library图书馆

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

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