在 Java 中播放 .mp3 和 .wav?
如何在 Java 应用程序中播放 .mp3
和 .wav
文件?我正在使用摇摆。我尝试在互联网上查找类似以下示例的内容:
public void playSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("D:/MusicPlayer/fml.mp3").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
但是,这只会播放 .wav
文件。
与:
http://www.javaworld.com/javaworld/javatips/jw 相同-javatip24.html
我希望能够用相同的方法播放 .mp3
文件和 .wav
文件。
How can I play an .mp3
and a .wav
file in my Java application? I am using Swing. I tried looking on the internet, for something like this example:
public void playSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("D:/MusicPlayer/fml.mp3").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
But, this will only play .wav
files.
The same with:
http://www.javaworld.com/javaworld/javatips/jw-javatip24.html
I want to be able to play both .mp3
files and .wav
files with the same method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
Java FX 有
Media
和MediaPlayer
类,可以播放 mp3 文件。示例代码:
您将需要以下导入语句:
Java FX has
Media
andMediaPlayer
classes which will play mp3 files.Example code:
You will need the following import statements:
我写了一个纯java mp3播放器:mp3transform。
I wrote a pure java mp3 player: mp3transform.
使用标准 javax.sound API、轻量级 Maven 依赖项、完全开源(需要 Java 7 或更高版本),这应该能够播放大多数 WAV、OGG Vorbis 和 MP3 文件:
pom.xml :
代码:
参考文献:
Using standard javax.sound API, lightweight Maven dependencies, completely Open Source (Java 7 or later required), this should be able to play most WAVs, OGG Vorbis and MP3 files:
pom.xml:
Code:
References:
您只能使用 java API 播放 .wav:
代码:
并使用 jLayer 播放 .mp3
you can play .wav only with java API:
code:
And play .mp3 with jLayer
我已经有一段时间没有使用它了,但是 JavaLayer 非常适合 MP3 播放
It's been a while since I used it, but JavaLayer is great for MP3 playback
我建议使用 BasicPlayerAPI。它是开源的,非常简单,并且不需要 JavaFX。
http://www.javazoom.net/jlgui/api.html
下载并解压 zip 文件后应将以下 jar 文件添加到项目的构建路径中:
这是简约的用法示例:
所需的导入:
这就是开始播放音乐所需的全部内容。播放器正在启动和管理自己的播放线程,并提供播放、暂停、恢复、停止和搜索功能。
对于更高级的用法,您可以查看 jlGui 音乐播放器。它是一个开源 WinAmp 克隆: http://www.javazoom.net/jlgui/jlgui.html
第一个要查看的类是 PlayerUI (在 javazoom.jlgui.player.amp 包内)。
它很好地展示了 BasicPlayer 的高级功能。
I would recommend using the BasicPlayerAPI. It's open source, very simple and it doesn't require JavaFX.
http://www.javazoom.net/jlgui/api.html
After downloading and extracting the zip-file one should add the following jar-files to the build path of the project:
Here is a minimalistic usage example:
Required imports:
That's all you need to start playing music. The Player is starting and managing his own playback thread and provides play, pause, resume, stop and seek functionality.
For a more advanced usage you may take a look at the jlGui Music Player. It's an open source WinAmp clone: http://www.javazoom.net/jlgui/jlgui.html
The first class to look at would be PlayerUI (inside the package javazoom.jlgui.player.amp).
It demonstrates the advanced features of the BasicPlayer pretty well.
我发现最简单的方法是从 http://www.javazoom.net/ 下载 JLayer jar 文件javalayer/sources.html 并将其添加到 Jar 库 http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29
这是该类的代码
这是进口的
The easiest way I found was to download the JLayer jar file from http://www.javazoom.net/javalayer/sources.html and to add it to the Jar library http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29
Here is the code for the class
and here are the imports
为了给读者提供另一种选择,我建议使用 JACo MP3 Player 库,一个跨平台的 java mp3 播放器。
特点:
有关其方法和属性的完整列表,您可以查看其文档 在这里。
示例代码:
有关更多详细信息,我创建了一个简单的教程 这里包含可下载的源代码。
更新(2022)
该页面的下载链接不再有效,但有一个 sourceforge 项目
To give the readers another alternative, I am suggesting JACo MP3 Player library, a cross platform java mp3 player.
Features:
For a complete list of its methods and attributes you can check its documentation here.
Sample code:
For more details, I created a simple tutorial here that includes a downloadable sourcecode.
UPDATE(2022)
The download link at that page does not work anymore, but there is a sourceforge project
使用 MP3 解码器/播放器/转换器 Maven 依赖项。
Using MP3 Decoder/player/converter Maven Dependency.
您需要先安装 JMF(使用此链接下载
)不要忘记添加 JMF jar 文件
You need to install JMF first (download using this link)
don't forget to add JMF jar files
在 freshmeat.net 上搜索 JAVE(Java 音频视频编码器)库(链接此处)。这是一个存放此类内容的图书馆。不知道Java有没有原生的mp3功能。
如果您想要一种方法来运行两种类型的文件,您可能需要使用继承和简单的包装函数将 mp3 函数和 wav 函数包装在一起。
Do a search of freshmeat.net for JAVE (stands for Java Audio Video Encoder) Library (link here). It's a library for these kinds of things. I don't know if Java has a native mp3 function.
You will probably need to wrap the mp3 function and the wav function together, using inheritance and a simple wrapper function, if you want one method to run both types of files.
要向 Java Sound 添加 MP3 读取支持,请将 JMF 的
mp3plugin.jar
添加到应用程序的运行时类路径中。请注意,
Clip
类具有内存限制,因此不适合超过几秒的高质量声音。To add MP3 reading support to Java Sound, add the
mp3plugin.jar
of the JMF to the run-time class path of the application.Note that the
Clip
class has memory limitations that make it unsuitable for more than a few seconds of high quality sound.我还有其他方法,第一个是:
第二个是:
如果我们想循环播放此音频,我们可以使用此方法。
如果我们想停止这个循环,我们在 try 中添加这个库:
对于第三种方法,我们添加以下导入:
I have other methods for that, the first is :
And the second is :
And if we want to make loop to this audio we use this method.
if we want to stop this loop we add this libreries in the try:
for this third method we add the folowing imports :
什么都没起作用。但这是完美的
Nothing worked. but this one perfectly ????
google and download Jlayer library first.
使用这个库: import sun.audio.*;
Use this library: import sun.audio.*;