【发布时间】:2014-02-05 02:59:43
【问题描述】:
我已经找到了很多关于这个主题的主题,但我仍然无法让它发挥作用。它可以从编译器轻松工作,但不能从 jar 文件中工作。 jar 文件似乎找到了音频文件,但它只是不播放它。
import sun.audio.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
@SuppressWarnings({"serial","restriction"})
public class JarSoundTest1 extends JFrame {
JButton button;
InputStream in;
AudioStream as;
public JarSoundTest1() throws Exception {
JOptionPane.showMessageDialog(null, this.getClass().getResource("blopp.wav"));
button = new JButton("Click to Blopp!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
in = this.getClass().getResourceAsStream("blopp.wav");
as = new AudioStream (in);
AudioPlayer.player.start(as);
JOptionPane.showMessageDialog(null, "try");
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "catch");
}
}
});
add(button);
}
public static final void main(String[] args) throws Exception {
JFrame frame = new JarSoundTest1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
每次单击按钮时,JOptionPane 都会显示“尝试”,这表明我假设找到了文件?仍然没有声音播放。我尝试使用放置在 jar 文件内部和外部的音频文件。非常感谢您帮助理解这一点。
【问题讨论】:
-
注意,使用 javax.sound.sampled 可能比使用 sun 的更好,因为随着时间的推移,这些实现无法保证正确性。