Java 媒体框架 - MP3 问题

标签 java mp3 jmf

我使用的是 Windows 7,可以在混音器中使用“Java Platform SE Binary”,但似乎仍然没有声音播放。

我的代码是:

import javax.media.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLExc;

public class SimpleAudioPlayer {
private Player audioPlayer = null;

public SimpleAudioPlayer(URL url) throws IOException, NoPlayerException, 
    CannotRealizeException {
    audioPlayer = Manager.createRealizedPlayer(url);
}

public SimpleAudioPlayer(File file) throws IOException, NoPlayerException, 
    CannotRealizeException {
    this(file.toURL());
}

public void play() {
    audioPlayer.start();
}

public void stop() {
    audioPlayer.stop();
    audioPlayer.close();
}

public static void main(String[] args) {
    try{
        File audioFile = new File("/t.mp3");
        SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile);

        System.out.println();
        System.out.println("-> Playing file '" + 
                           audioFile.getAbsolutePath() + "'");
        System.out.println("   Press the Enter key to exit");
        player.play();

        // wait for the user to press Enter to proceed.
        System.in.read();
        System.out.println("-> Exiting");
        player.stop();
    }catch(Exception ex){
        ex.printStackTrace();
    }

    System.exit(0);

}
}

我使用 Windows 性能 JMF 版本。我尝试播放的 MP3 在 VLC/WMP 中工作正常,因此它不可能是该文件。

该代码在运行时也没有抛出任何异常或错误,它只是似乎没有播放声音。

我有什么遗漏吗?比如拔声卡?例如。接管它以便我可以播放声音?

我的总体目标是使用 RTP/RTSP 提供 MP3 流媒体服务,因此任何链接、建议或教程都会有所帮助,因为我当前使用 IBM JMF TuturiolJava Demo

请询问是否需要更多信息!

更新-

已下载WAV FILE它似乎可以播放,我怎样才能让 MP3 播放?

添加了格式并尝试了此代码,但仍然存在相同的问题:

import java.io.File;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;

public class SimpleAudioPlayer {
    public static void main(String[] args) {

        Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
        Format input2 = new AudioFormat(AudioFormat.MPEG);

        Format output = new AudioFormat(AudioFormat.LINEAR);
        PlugInManager.addPlugIn(
            "com.sun.media.codec.audio.mp3.JavaDecoder",
            new Format[]{input1, input2},
            new Format[]{output},
            PlugInManager.CODEC
        );
        try {
            Player player = Manager.createPlayer(new MediaLocator(new File("/t.mp3").toURI().toURL()));
            player.start();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

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@62deaa2e Error: Unable to realize com.sun.media.PlaybackEngine@62deaa2e

这就是错误!

最佳答案

正如我所想,这是缺少编解码器。

我认为这就是您所需要的:http://www.oracle.com/technetwork/java/javase/download-137625.html

关于Java 媒体框架 - MP3 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080606/

相关文章:

java - 如何在 Android 中没有 UI 或用户交互的情况下拍摄大量照片?

java - 在 jdbc url 中设置用户变量

android - 如何将 mediastore.audio 用于专辑和艺术家信息并将其添加到自定义 ListView 中?

android MediaPlayer 不播放 mp3 文件

java - 我的.jar文件无法打开MP3文件(我使用的是Jlayer-JZoom库)

java - Java 中的视频播放(JMF、Fobs4JMF、Xuggler、FMJ)

java - 从 txt.file 读取并使用 java 打印出其二维数字

java - Spring数据JPA中的排序表达式

java - 使用jmf播放视频

java - 嵌入式资源(媒体文件)在 jmf 和 Netbeans 中播放,但在制作项目的 jar 后视频无法播放