我使用的是 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 Tuturiol和Java 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/