![](/img/trans.png)
[英]Java sound replaying via Clip and AudioInputStream is not working
[英]AudioInputStream is not working
我每次用戶按下按鈕時都會嘗試播放.wav聲音,但會拋出異常:
Exception in thread "Thread-0" java.lang.IllegalArgumentException: Invalid format at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142) at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99) at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283) at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402) at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453) at Uber.play(Uber.java:534) at Uber$5.run(Uber.java:340) at java.lang.Thread.run(Thread.java:724)
這是代碼:
//Play Audio File
public void play(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException
{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(file));
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
}
我設法讓它運作起來。 這是我使用的代碼。 請記住,我需要這個只是為了播放一個短的beep.wav聲音。 對於較長的聲音文件似乎有些麻煩。 讓我知道它是否適合你們,如果你設法用這段代碼播放更長的聲音。
public void play(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException
{
try
{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(file));
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(inputStream);
clip.start();
}
catch (IOException | LineUnavailableException | UnsupportedAudioFileException e1)
{
e1.printStackTrace();
}
}
你傳遞的文件路徑有問題。 當我使用相同的代碼從JFileChooser
獲取文件時,它工作正常。 測試一下。
另請參閱Javasound wiki標記以處理不支持的音頻文件類型
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.SwingUtilities;
public class TestAudio {
public TestAudio() {
JButton button = new JButton("Choose file");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
File file = null;
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
String fileName = file.getAbsolutePath();
try {
play(fileName);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
JFrame frame = new JFrame();
frame.add(button);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(300, 300);
frame.setVisible(true);
}
public void play(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(file));
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestAudio();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.