在Java中播放.mp3和.wav?
如何在Java应用程序中播放在Java中播放.mp3和.wav?,java,audio,mp3,wav,Java,Audio,Mp3,Wav,如何在Java应用程序中播放.mp3和.wav文件?我在用秋千。我试着在互联网上查找类似以下示例的内容: public void playSound() { try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("D:/MusicPlayer/fml.mp3").getAbsoluteFile()); Clip clip = AudioSyste
.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
文件
同样适用于:
我希望能够使用相同的方法播放
.mp3
文件和.wav
文件。搜索freshmeat.net中的JAVE(代表Java音频视频编码器)库(链接)。这是一个图书馆,收藏这类东西。我不知道Java是否有本机mp3功能
如果您想要一种方法来运行这两种类型的文件,您可能需要使用继承和简单的包装函数将mp3函数和wav函数包装在一起。我已经有一段时间没有使用它了,但是对于mp3播放来说非常好为Java声音添加mp3阅读支持,将JMF的
mp3plugin.jar
添加到应用程序的运行时类路径中
请注意,
Clip
类有内存限制,不适合播放超过几秒钟的高质量声音。我编写了一个纯java mp3播放器:。java FX有媒体
和媒体播放器
类,它们将播放mp3文件
示例代码:
String bip = "bip.mp3";
Media hit = new Media(new File(bip).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
您将需要以下导入语句:
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
您需要先安装JMF()
不要忘记添加JMF jar文件您只能使用java API播放.wav:
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
代码:
使用jLayer播放.mp3,使用标准的javax.sound API,一个独立的Maven依赖项,完全开源(Java 7或更高版本),这应该能够播放大多数WAV、OGG Vorbis和mp3文件: pom.xml:
<!--
We have to explicitly instruct Maven to use tritonus-share 0.3.7-2
and NOT 0.3.7-1, otherwise vorbisspi won't work.
-->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>tritonus-share</artifactId>
<version>0.3.7-2</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5-1</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>vorbisspi</artifactId>
<version>1.0.3-1</version>
</dependency>
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
public class AudioFilePlayer {
public static void main(String[] args) {
final AudioFilePlayer player = new AudioFilePlayer ();
player.play("something.mp3");
player.play("something.ogg");
}
public void play(String filePath) {
final File file = new File(filePath);
try (final AudioInputStream in = getAudioInputStream(file)) {
final AudioFormat outFormat = getOutFormat(in.getFormat());
final Info info = new Info(SourceDataLine.class, outFormat);
try (final SourceDataLine line =
(SourceDataLine) AudioSystem.getLine(info)) {
if (line != null) {
line.open(outFormat);
line.start();
stream(getAudioInputStream(outFormat, in), line);
line.drain();
line.stop();
}
}
} catch (UnsupportedAudioFileException
| LineUnavailableException
| IOException e) {
throw new IllegalStateException(e);
}
}
private AudioFormat getOutFormat(AudioFormat inFormat) {
final int ch = inFormat.getChannels();
final float rate = inFormat.getSampleRate();
return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}
private void stream(AudioInputStream in, SourceDataLine line)
throws IOException {
final byte[] buffer = new byte[4096];
for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);
}
}
}
参考资料:
<!--
We have to explicitly instruct Maven to use tritonus-share 0.3.7-2
and NOT 0.3.7-1, otherwise vorbisspi won't work.
-->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>tritonus-share</artifactId>
<version>0.3.7-2</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5-1</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>vorbisspi</artifactId>
<version>1.0.3-1</version>
</dependency>
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
public class AudioFilePlayer {
public static void main(String[] args) {
final AudioFilePlayer player = new AudioFilePlayer ();
player.play("something.mp3");
player.play("something.ogg");
}
public void play(String filePath) {
final File file = new File(filePath);
try (final AudioInputStream in = getAudioInputStream(file)) {
final AudioFormat outFormat = getOutFormat(in.getFormat());
final Info info = new Info(SourceDataLine.class, outFormat);
try (final SourceDataLine line =
(SourceDataLine) AudioSystem.getLine(info)) {
if (line != null) {
line.open(outFormat);
line.start();
stream(getAudioInputStream(outFormat, in), line);
line.drain();
line.stop();
}
}
} catch (UnsupportedAudioFileException
| LineUnavailableException
| IOException e) {
throw new IllegalStateException(e);
}
}
private AudioFormat getOutFormat(AudioFormat inFormat) {
final int ch = inFormat.getChannels();
final float rate = inFormat.getSampleRate();
return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}
private void stream(AudioInputStream in, SourceDataLine line)
throws IOException {
final byte[] buffer = new byte[4096];
for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);
}
}
}
- BasicLayer3.0.jar
- all来自lib目录的JAR(在BasicLayer3.0中)
String songName = "HungryKidsofHungary-ScatteredDiamonds.mp3";
String pathToMp3 = System.getProperty("user.dir") +"/"+ songName;
BasicPlayer player = new BasicPlayer();
try {
player.open(new URL("file:///" + pathToMp3));
player.play();
} catch (BasicPlayerException | MalformedURLException e) {
e.printStackTrace();
}
所需进口:
import java.net.MalformedURLException;
import java.net.URL;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;
这就是你开始演奏音乐所需要的一切。播放器正在启动和管理自己的播放线程,并提供播放、暂停、恢复、停止和搜索功能
有关更高级的用法,请查看jlGui音乐播放器。这是一个开源WinAmp克隆:
要查看的第一个类是PlayerUI(在javazoom.jlgui.player.amp包中)。
它很好地演示了BasicLayer的高级功能。我找到的最简单的方法是从下载JLayer jar文件并将其添加到jar库中 这是这个类的代码
public class SimplePlayer {
public SimplePlayer(){
try{
FileInputStream fis = new FileInputStream("File location.");
Player playMP3 = new Player(fis);
playMP3.play();
} catch(Exception e){
System.out.println(e);
}
}
}
这是进口货
import javazoom.jl.player.*;
import java.io.FileInputStream;
为了给读者提供另一种选择,我建议使用jacomp3播放器库,一种跨平台的javamp3播放器 特点:
- CPU使用率非常低(~2%)
- 难以置信的小图书馆(约90KB)
- 不需要JMF(Java媒体框架)
- 易于集成到任何应用程序中
- 易于集成到任何网页中(作为小程序)
import jaco.mp3.player.MP3Player;
import java.io.File;
public class Example1 {
public static void main(String[] args) {
new MP3Player(new File("test.mp3")).play();
}
}
有关更多详细信息,我创建了一个简单的教程,其中包含可下载的源代码。使用此库:import sun.audio.*
public void Sound(String Path){
try{
InputStream in = new FileInputStream(new File(Path));
AudioStream audios = new AudioStream(in);
AudioPlayer.player.start(audios);
}
catch(Exception e){}
}
使用Maven依赖项
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class PlayAudio{
public static void main(String[] args) throws FileNotFoundException {
try {
FileInputStream fileInputStream = new FileInputStream("mp.mp3");
Player player = new Player((fileInputStream));
player.play();
System.out.println("Song is playing");
while(true){
System.out.println(player.getPosition());
}
}catch (Exception e){
System.out.println(e);
}
}
}
我还有其他方法,第一种是:
public static void playAudio(String filePath){
try{
InputStream mus = new FileInputStream(new File(filePath));
AudioStream aud = new AudioStream(mus);
}catch(Exception e){
JOptionPane.showMessageDialig(null, "You have an Error");
}
第二个是:
try{
JFXPanel x = JFXPanel();
String u = new File("021.mp3").toURI().toString();
new MediaPlayer(new Media(u)).play();
} catch(Exception e){
JOPtionPane.showMessageDialog(null, e);
}
如果我们想循环这个音频,我们就用这个方法
try{
AudioData d = new AudioStream(new FileInputStream(filePath)).getData();
ContinuousAudioDataStream s = new ContinuousAudioDataStream(d);
AudioPlayer.player.start(s);
} catch(Exception ex){
JOPtionPane.showMessageDialog(null, ex);
}
如果要停止此循环,请在try中添加以下库:
AudioPlayer.player.stop(s);
对于第三种方法,我们添加以下导入:
import java.io.FileInputStream;
import sun.audio.AudioData;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
什么都没用。但是这个我真的不知道如何使用自定义库,有什么帮助吗?下载库并在代码中编写include语句。应包括图书馆使用说明。通常,函数调用就足够了,尽管您可能需要先声明一个对象。然后,创建一个检查其输入的文件扩展名的函数,并调用所需的库函数。最终URL resource=getClass().getResource(“a.mp3”);这对我根本不起作用。它说进口品不存在。我正在运行Java7。。。如果使用Eclipse,似乎需要从Java 7文件夹中手动添加javafx库。从技术上讲,
Media
和MediaPlayer
不是Java类,而是javafx类。要在OS X或Windows上向Java添加mp3支持,您可能需要查看。是的,我编写了这些库。别忘了你也需要初始化javaFX才能工作。是的,很酷。简单且不依赖于平台。在后台运行良好,只需了解如何停止线程。请注意,很久以前Sun/Oracle就放弃了JMF。JMF是在2003年被放弃的。不建议您使用它。@mathguy54当我建议人们不要使用它时,是因为它不支持足够多的不同类型的媒体。不过,它仍然完全可以解码MP3。谢谢,这是我尝试过的向当前应用程序添加MP3支持的最简单的方法mp3spi1.9.4.jar
应替换为java zoom站点中的mp3spi1.9.5.jar
。不要忘记在player.play()
之后睡眠主线程,否则您可能听不到任何声音。是的,它确实检查了此视频[它通过了所有的函数,如停止和暂停。我认为它应该已经包含了这些函数,因为它们看起来相当基本。无论如何,我找到了另一个库的解决方案,谢谢你的回答:)这是错误的。Java将播放除wav之外的其他容器格式。此外,wav是一种容器格式,甚至可以包含mp3。所以Java可以