我似乎无法弄清楚为什么我的音频文件无法播放。音频文件是一个wav文件,只是。我得到的错误是javax.sound.sampled.UnsupportedAudioFileException
。
public class MusicProgress {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame b = new JFrame();
FileDialog fd = new FileDialog(b, "Pick a file: ", FileDialog.LOAD);
fd.setVisible(true);
final File file = new File(fd.getDirectory() + fd.getFile());
//URI directory = new URI (fd.getDirectory() + fd.getFile());
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
AudioFormat audioFormat = inputStream.getFormat();
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
我挖出了一些我的旧代码来播放音频文件。这可能有效:
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;
public class SoundPlayer {
Clip clip;
public SoundPlayer(String file){
//if(clip.isRunning()){clip.stop();}
try{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(SoundPlayer.class.getResource(file));
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}catch(Exception err){err.printStackTrace(); JOptionPane.showMessageDialog(null, "SoundPlayer: "+err,null,0);}
}
}
播放音频片段:
new SoundPlayer(filepath);
路径应该看起来像这样:“/ game/resources/sound/Explosion.wav” 正如你所说,它必须是.wav文件。
答案 1 :(得分:0)
您必须使用外部库来播放.mp3
等文件仅支持Java .wav
但这还不够。你需要的是一个外部算法来播放其他音乐格式。所有其他格式来自.wav,它们会传递给算法,然后繁荣它们成为.ogg, .MP3,.whatever
1.一个令人印象深刻的图书馆使用哪个支持.mp3 JLayer.jar 您可以将此jar作为外部库导入项目中。
2.如果你搜索更多,你会发现JAudiotagger它真的很棒但很难使用。
3.此外,您可以使用Java Media FrameWork,但不管它支持多种格式。
4.JavaZoom还有其他库支持 .ogg,.speex,.flac,.mp3
How to play .wav files with java
上的stackoverflow链接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();
}
}
答案 2 :(得分:0)
Java不支持所有wav格式。支持的最高质量是标准CD编码格式。有了这些文件,你应该没问题。该格式具有16位编码,44100 fps,小端,立体声。您应该能够检查wav文件的属性并了解它是否匹配。
DAW越来越常见的是生成具有24位或32位编码或48000fps或92000fps的wav文件。
可以将这些转换为" CD"使用Audacity等工具编码规范。