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;

}

[13591 byte] By [Carsten78a] at [2007-11-27 2:07:44]
# 1
So maybe it would have been a good idea to print the exception's stack traces in the catch blocks instead of throwing them away and printing some rather error unrelated statement...You deserve every bit of trouble you get.
CeciNEstPasUnProgrammeura at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 2
Please come back when you can provide a complete exception message witth stack trace.
CeciNEstPasUnProgrammeura at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 3
Maybe i should have posted this in the "New to java" forum. Maybe then i would have gotten an answer a little less hostile.I had to look up Stack traces at wikipedia. I guess your obnoxious answer guided me in the right direction after all. /Carsten
Carsten78a at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 4
You're welcome. Although you would have gotten the very same reply in "New to Java" as well.So now, what's the stack trace so we can actually work on the issue?
CeciNEstPasUnProgrammeura at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 5

In case looking it up didn't help you as much:

catch(Exception ex) {

ex.printStackTrace();

}

is lots better than printing some message. It's like going to the doctor and telling him "body part xyz hurts with a stinging pain whenever I do the following..." instead of saying "Doc, there's something wrong".

CeciNEstPasUnProgrammeura at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 6

i tried with e.printStackTrace();

and the outpur was:

"mark/reset not supported"

I guess thats pretty self explanatory - now im uncertain if i really need to reset the inFileAIS.

And if mark/reset is not supported, what other methods can be invoked to restart an audioinputstream?

/Carsten

Carsten78a at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 7
I hope you see the point I was making now.I don't really see why you're resetting it at either, a new stream should always start at the beginning. To restart it, just close the stream and create a new instance.
CeciNEstPasUnProgrammeura at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 8
I see the point, and appreciate your help very much.Thanks/Carsten
Carsten78a at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 9

By the way: don't catch NullPointerExceptions (or other RuntimeExceptions) unless there's a good reason. Either data that may not be present isn't present, in that case it's nothing surprising and should be just checked instead of being blindly accessed, or your code has a bug which shouldn't be hidden.

CeciNEstPasUnProgrammeura at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...
# 10
ok
Carsten78a at 2007-7-12 1:56:16 > top of Java-index,Java Essentials,Java Programming...