我正在使用Eclipse保存.wav文件,大约每四或五次运行程序时它会保存文件。大多数情况下,当文件选择器框架变得可见时,程序本身只会挂起并且屏幕变黑,以选择文件的位置。有谁知道为什么会偶尔发生这种情况? Eclipse在控制台窗口中没有出错,代码构建良好且没有错误。 StopRecording和saveFile都在Mainframe类中 save方法在另一个录制设置类
中private void stopRecording() {
isRecording = false;
try {
timer.cancel();
RecordButton.setText("Record");
RecordButton.setIcon(iconRecord);
recorder.stop();
saveFile();
} catch (IOException ex) {
JOptionPane.showMessageDialog(Mainframe.this, "Error",
"Error stopping sound recording!",
JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
FileFilter wavFilter = new FileFilter() {
@Override
public String getDescription() {
return "Sound file (*.WAV)";
}
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
return file.getName().toLowerCase().endsWith(".wav");
}
}
};
fileChooser.setFileFilter(wavFilter);
fileChooser.setAcceptAllFileFilterUsed(false);
int userChoice = fileChooser.showSaveDialog(this);
if (userChoice == JFileChooser.APPROVE_OPTION) {
saveFilePath = fileChooser.getSelectedFile().getAbsolutePath();
if (!saveFilePath.toLowerCase().endsWith(".wav")) {
saveFilePath += ".wav";
}
File wavFile = new File(saveFilePath);
try {
recorder.save(wavFile);
buttonPlay.setEnabled(true);
Keyup.setEnabled(true);
Keydown.setEnabled(true);
btnSave.setEnabled(true);
getKey.setEnabled(true);
System.out.print(saveFilePath);
} catch (IOException ex) {
JOptionPane.showMessageDialog(Mainframe.this, "Error",
"Error saving to sound file!",
JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
}
public void save(File wavFile) throws IOException {
byte[] audioData = recordBytes.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
AudioInputStream audioInputStream = new AudioInputStream(bais, format,
audioData.length / format.getFrameSize());
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, wavFile);
audioInputStream.close();
recordBytes.close();
}
答案 0 :(得分:0)
您正在调用recorder.save(wavFile);两次。尝试只调用一次。