0
我試圖製作一個將聲音加載到按鈕中,然後在按下按鈕時播放聲音的音板程序。這是我迄今爲止 -在Java中播放音頻
package soundboard;
import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;
public class Soundboard {
JButton loadButton;
JFileChooser loadBox;
JButton clearButton;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JPanel mainsPanel;
int load;
Clip clip;
File one;
File two;
File three;
File four;
public void windowCreate() {
JFrame frame = new JFrame();
mainsPanel = new JPanel();
loadBox = new JFileChooser();//174
loadBox.setSize(500,500);
loadBox.setLocation(174,4);
loadButton = new JButton("Load...");
loadButton.setSize(80, 30);
loadButton.setLocation(4, 4);
loadButton.addActionListener(e -> {
load = 1;
});
clearButton = new JButton("Clear");
clearButton.setSize(80, 30);
clearButton.setLocation(92, 4);
clearButton.addActionListener(e -> {
System.out.println("Cleared");
});
button1 = new JButton("1");
button1.setSize(80, 80);
button1.setLocation(4, 45);
button1.addActionListener(e -> {
if (load == 1){
one = loadBox.getSelectedFile();
load = 0;
}
else {
System.out.println("Debugging; " + one);
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(one);
AudioFormat format = audio.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
}
catch (LineUnavailableException ex) {
System.out.println("Err_Line");
}
catch (IOException ex) {
System.out.println("Err_IOException");
}
catch (UnsupportedAudioFileException ex) {
System.out.println("Err_FileNotSupported");
}
clip.setFramePosition(0);
clip.start();
}
});
button2 = new JButton("2");
button2.setSize(80, 80);
button2.setLocation(92, 45);
button2.addActionListener(e -> {
if (load == 1){
two = loadBox.getSelectedFile();
load = 0;
}
else {
System.out.println(two);
}
});
button3 = new JButton("3");
button3.setSize(80, 80);
button3.setLocation(4, 133);
button3.addActionListener(e -> {
if (load == 1){
three = loadBox.getSelectedFile();
load = 0;
}
else {
}
});
button4 = new JButton("4");
button4.setSize(80, 80);
button4.setLocation(92, 133);
button4.addActionListener(e -> {
if (load == 1){
four = loadBox.getSelectedFile();
load = 0;
}
else {
System.out.println(four);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(loadButton);
frame.add(clearButton);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(loadBox);
frame.add(mainsPanel);
frame.setSize(675,485);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args){
Soundboard window = new Soundboard();
window.windowCreate();
}
}
我需要什麼,基本上是播放聲音時,按下「按鈕1」,而不會彈出一個對外窗口某種方式,而無需編寫的新方法,而不必走出行動監聽器之外。複製粘貼到一個名爲「Soundboard」的新項目中,如果你知道這一點,請告訴我。
這是一個測試聲音使用 - https://drive.google.com/file/d/0B3riImENY0zSMUItWkUtaUg1X3M/edit?usp=sharing
不是重複的 - 它有點不同 – ob103ninja 2014-09-24 13:16:03
好吧,如果你不想將其他問題的方法添加到你的代碼中,你所要做的就是複製並粘貼其他代碼。 – APerson 2014-09-24 13:16:59
doesn'我看起來是重複的。同一主題,不同的問題。 – seinecle 2014-09-24 13:17:22