我在Stack Overflow上找到了一些可能的答案,但它们都很老了,看起来它们使用的是过时的技术。
我需要播放一个给定绝对文件名的mp3文件。
这是我尝试过的:
1。JavaFX
MediaPlayer player = new MediaPlayer(new Media(uriString));
我得到了java.lang.IllegalStateException: Toolkit not initialized
。
我可能会找到一种方法来初始化这个工具包,但我想知道这是否是首选的方法。
2。Intellij UIUtil
final InputStream is = new FileInputStream(fileName);
UIUtil.playSoundFromStream(new Factory<InputStream>() {
@Override
public InputStream create() {
return is;
}
});
我得到Audio format is not yet supported: could not get audio input stream from input file
我做了更多的尝试,但这是我的记录。
到目前为止,唯一对我有效的是在Mac上从shell:播放文件,
Runtime.getRuntime().exec("afplay " + filePath);
但是我更喜欢Java解决方案。什么好主意吗?
-
JavaFX
你可以在这里看看使用javafx获取mp3文件播放
-
给你,我最喜欢的部分:
你可以使用JLayer支持.mp3.
例子
new Thread(()->{
try {
FileInputStream file = new FileInputStream("path ..../audio.mp3"); //initialize the FileInputStream
Player player= new Player(file); //initialize the player
player.play(); //start the player
} catch (Exception e) {
e.printStackTrace();
}
}).start();
注意:
请注意,我使用单独的Thread
,因为如果不是,应用程序将堆栈。
一般来说:
你必须使用外部库来播放文件,如。mp3在Java
(虽然JavaFX
支持。mp3,但不是所有格式)
Java只支持。wav
尽管这已经足够了。你所需要的只是一个外部算法来播放其他音乐格式。所有其他格式最初都来自。wav,它们被传递到一个算法中然后它们就变成了.ogg,.mp3,.whatever
1。如前所述,.mp3
JLayer.jar您可以将此jar作为外部库导入到您的项目中。
2。JavaZoom也有其他库支持.ogg,.speex,.flac,.mp3,点击上面的链接下载jlGui
项目,你可以找到很多格式的库。
链接到stackoverflow on如何用java播放。wav文件
和http://alvinalexander.com/java/java-audio-example-java-au-play-sound不确定是否仍然适用于java 8
代码:
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.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioPlayerExample1 implements LineListener {
/**
* this flag indicates whether the playback completes or not.
*/
boolean playCompleted;
/**
* Play a given audio file.
* @param audioFilePath Path of the audio file.
*/
void play() {
File audioFile = new File("C:/Users/Alex.hp/Desktop/Musc/audio.wav");
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioStream);
audioClip.start();
while (!playCompleted) {
// wait for the playback completes
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
audioClip.close();
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
/**
* Listens to the START and STOP events of the audio line.
*/
@Override
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.START) {
System.out.println("Playback started.");
} else if (type == LineEvent.Type.STOP) {
playCompleted = true;
System.out.println("Playback completed.");
}
}
public static void main(String[] args) {
AudioPlayerExample1 player = new AudioPlayerExample1();
player.play();
}
}