为什么我在尝试播放 mp3 文件时会出现这些异常?
我下载了 JMF MP3 插件 在我的 java 中播放 mp3 文件我将 mp3plugin.jar
与 JDK 一起包含在我的 netbeans 项目库中。但我仍然无法播放 mp3 文件。 当我运行代码时,出现以下异常:
Exception in thread "main" javax.sound.sampled.LineUnavailableException: line with format MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, unknown frame rate, not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:541)
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1341)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:124)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1121)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1211)
at mp3.MP3.main(MP3.java:25)
Java Result: 1
这是我编写的用于播放 MP3 文件的代码
import javax.sound.sampled.*;
import java.io.*;
public class MP3 {
static Thread th;
public static void main(String[] args) throws Exception {
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("mysong.mp3"));
clip.open(ais);
clip.loop(0);
long tf = (long)(clip.getFrameLength() * clip.getFormat().getFrameRate());
Thread.sleep( ( tf* 1000 ));
}
}
为什么会出现这些异常?是不是代码有问题。
I downloaded JMF MP3 Plugin to play mp3 files in my java program.I included mp3plugin.jar
in my project library of netbeans along with JDK. But still i am unable to play mp3 file.
When i run my code i get the following exception :
Exception in thread "main" javax.sound.sampled.LineUnavailableException: line with format MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, unknown frame rate, not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:541)
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1341)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:124)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1121)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1211)
at mp3.MP3.main(MP3.java:25)
Java Result: 1
THIS IS THE CODE THAT I HAVE WRITTEN TO PLAY MP3 FILES
import javax.sound.sampled.*;
import java.io.*;
public class MP3 {
static Thread th;
public static void main(String[] args) throws Exception {
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("mysong.mp3"));
clip.open(ais);
clip.loop(0);
long tf = (long)(clip.getFrameLength() * clip.getFormat().getFrameRate());
Thread.sleep( ( tf* 1000 ));
}
}
Why do i get these exceptions ? Is there a problem with the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您参考此链接作为示例。
http://blogs.oracle.com/kashmir/entry/java_sound_api_2_mp3
基本上是 MP3 插件可以读取 MP3,但不能播放。为了播放它们,您必须在程序中将它们转换为实际可以播放的文件类型,例如 PCM。
I refer you to this link for an example.
http://blogs.oracle.com/kashmir/entry/java_sound_api_2_mp3
Basically the MP3 plugin can read in MP3s, but not play them. In order to play them you must convert them in the program to a file type that can actually be played such as PCM.