1
我在这个主题上发现了很多线程,但我仍然没有得到它的工作。它很容易从编译器中运行,但不能从jar文件运行。看起来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更好,因为这些实现不能保证随着时间的推移正确性。 – demongolem 2014-02-04 18:33:08