import sun.audio.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SoundTest extends JFrame {
private static final long serialVersionUID = 1L;
JButton start;
JLabel lbl;
File filename;
FileInputStream in;
AudioStream as;
String path="/1.wav";
public SoundTest() {
try {
filename = new File(this.getClass().getResource(path).getPath());
in = new FileInputStream(filename);
as = new AudioStream(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
lbl = new JLabel(filename.getPath());
start = new JButton("開始");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ("開始".equals(e.getActionCommand())) {
AudioPlayer.player.start(as);
start.setText("結束");
} else if ("結束".equals(e.getActionCommand())) {
AudioPlayer.player.stop(as);
start.setText("開始");
}
}
});
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(lbl,BorderLayout.NORTH);
this.getContentPane().add(start,BorderLayout.CENTER);
this.setSize(400,300);
this.setVisible(true);
}
public static void main(String[] args) {
new SoundTest();
}
}
但打包成Jar后,出错了。
java.io.FileNotFoundException: file:\C:\Server.jar!\1.wav (檔案名稱、目錄名稱或
磁碟區標籤語法錯誤。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at com.sound.SoundTest.<init>(SoundTest.java:27)
at com.sound.SoundTest.main(SoundTest.java:56)
[此贴子已经被作者于2007-5-31 17:18:18编辑过]