problems with sound convertion example
Hi
Below is my code for a soundconverter (or atleast the beginning of one). By now i have only gotten to the sound convertion example in the java tutorials.
I keep getting this error:
Error: failure attempting to read /home/carsten/Desktop/1-welcome.wav!
I have given the file a chmod 777 permission.
im on linux, but i guess that shouldnt matter.
Hope you can help me out
the problems are in the convertFile method
Thanks
Carsten
/*
* SoundConverterGUI.java
*
* Created on 23. april 2007, 13:05
*/
package soundconverter;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author carsten
*/
publicclass SoundConverterGUIextends javax.swing.JFrame{
/** Creates new form SoundConverterGUI */
public SoundConverterGUI(){
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
privatevoid initComponents(){
loadFileBtn =new javax.swing.JButton();
loadFileLabel =new javax.swing.JLabel();
loadFileField =new javax.swing.JTextField();
fileFormatLabel =new javax.swing.JLabel();
fileFormatCombo =new javax.swing.JComboBox();
convertBtn =new javax.swing.JButton();
seperator =new javax.swing.JSeparator();
jSeparator1 =new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
loadFileBtn.setText("Browse");
loadFileBtn.addActionListener(new java.awt.event.ActionListener(){
publicvoid actionPerformed(java.awt.event.ActionEvent evt){
chooseFile(evt);
}
});
loadFileLabel.setText("File to convert:");
fileFormatLabel.setText("File format to convert to:");
fileFormatCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"AIFF"}));
convertBtn.setText("Convert");
convertBtn.addActionListener(new java.awt.event.ActionListener(){
publicvoid actionPerformed(java.awt.event.ActionEvent evt){
convertFile(evt);
}
});
jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
javax.swing.GroupLayout layout =new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loadFileLabel)
.addComponent(seperator, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(loadFileField, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,false)
.addComponent(fileFormatCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fileFormatLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 343, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
.addComponent(convertBtn, 0, 0, Short.MAX_VALUE)
.addComponent(loadFileBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
.addGroup(layout.createSequentialGroup()
.addGap(310, 310, 310)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(loadFileLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loadFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(loadFileBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fileFormatLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fileFormatCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(convertBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
privatevoid convertFile(java.awt.event.ActionEvent evt){
//The format to be converted from
AudioFileFormat inFileFormat;
try{
outFile =new File(inFile.getParent()+"/"+
inFile.getName().substring(0, inFile.getName().lastIndexOf("."))+"."+
fileFormatCombo.getSelectedItem());
System.out.println(outFile);
}catch (NullPointerException ex){
System.out.println("Error: one of the parameters is null!");
return;
}
try{
// query file type
inFileFormat = AudioSystem.getAudioFileFormat(inFile);
System.out.println(AudioSystem.getAudioFileFormat(inFile));
if (inFileFormat.getType() != AudioFileFormat.Type.AIFF){
// inFile is not AIFF, so let's try to convert it.
AudioInputStream inFileAIS =
AudioSystem.getAudioInputStream(inFile);
inFileAIS.reset();// rewind
if (AudioSystem.isFileTypeSupported(
AudioFileFormat.Type.AIFF, inFileAIS)){
// inFileAIS can be converted to AIFF.
// so write the AudioInputStream to the
// output file.
AudioSystem.write(inFileAIS,
AudioFileFormat.Type.AIFF, outFile);
System.out.println("Successfully made AIFF file, "
+ outFile.getPath() +", from "
+ inFileFormat.getType() +" file, " +
inFile.getPath() +".");
inFileAIS.close();
return;// All done now
}else
System.out.println("Warning: AIFF conversion of "
+ inFile.getPath()
+" is not currently supported by AudioSystem.");
}else
System.out.println("Input file " + inFile.getPath() +
" is AIFF." +" Conversion is unnecessary.");
}catch (UnsupportedAudioFileException e){
System.out.println("Error: " + inFile.getPath()
+" is not a supported audio file type!");
return;
}catch (IOException e){
System.out.println("Error: failure attempting to read "
+ inFile.getPath() +"!");
return;
}
}
privatevoid chooseFile(java.awt.event.ActionEvent evt){
//Melder fejl hvis man fortryder
//Lav en check om man har valgt en fil
FileNameExtensionFilter fileFilter =new FileNameExtensionFilter(
"Allowed file formats","wav");
JFileChooser fileChooser =new JFileChooser();
fileChooser.setFileFilter(fileFilter);
int returnVal = fileChooser.showOpenDialog(this);
inFile = fileChooser.getSelectedFile();
loadFileField.setText(inFile.getName());
}
// Variables declaration - do not modify
private javax.swing.JButton convertBtn;
private javax.swing.JComboBox fileFormatCombo;
private javax.swing.JLabel fileFormatLabel;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JButton loadFileBtn;
private javax.swing.JTextField loadFileField;
private javax.swing.JLabel loadFileLabel;
private javax.swing.JSeparator seperator;
// End of variables declaration
//Fields
private File inFile;
private File outFile;
}

