Results 1 to 5 of 5
- 12-05-2012, 05:15 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
File Chooser and forward and Backward Slashes
Hello. I am hoping to write a short application that will play a sound file chosen by the user. I'm using File Chooser so the user can select a sound file to play, and the name and path of the selected sound file is used by the part of the code that plays a sound file. I'm using NetBeans to code this.
I have the following problem. The File Chooser getSelectedFile() method returns the file using backwards slashes (i.e. C:\User\documents\folder 1\ ..etc.). I think I need the file path in forward slashes for the part of the code that plays the sound file (i.e. C:/User/documents/folder 1/...etc). In a previous verson of this code that did not use the File Chooser, I had hard-coded the sound files using address with forward slashes. This code worked to play the hard-coded sound files.
In my current version of the code using the File Chooser, I get an error message "fi1: the system cannot find the file specified".
Any help is appreciated.
Here is the relevant code from NetBeans
Java Code:private void bPlayActionPerformed(java.awt.event.ActionEvent evt) { try { // Open an audio input stream File sound1 = new File("fi1"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(sound1); // Get a sound clip resource Clip clip = AudioSystem.getClip(); // Open audio clip and load sample from the audio input stream clip.open(audioIn); clip.loop(3); } catch (UnsupportedAudioFileException uae) { System.out.println(uae.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } catch (LineUnavailableException lue) { lue.printStackTrace(); } } private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) { // get file name and path if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File fi1 = fileChooser.getSelectedFile(); String aFi = fi1.getAbsolutePath(); // check the chosen file and path System.out.println(fileChooser.getSelectedFile()); System.out.println("fi1: " + fi1); System.out.println("aPath: " + aFi); } }
- 12-05-2012, 05:53 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: File Chooser and forward and Backward Slashes
printStackTrace() on all your exceptions and find out exactly what error you are getting.
You do not need to do anything at all to the path returned from the file chooser.
It's already found a file in that location.
Except you aren't actually doing anything (useful) with that File.
It goes out of scope a few lines after you get it.
It never gets anywhere near the bPlayActionPerformed code.Please do not ask for code as refusal often offends.
- 12-05-2012, 07:06 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
Re: File Chooser and forward and Backward Slashes
Hello. Thanks for your reply.
I added printStackTrace() under each of the three exceptions in the bPlayActionPerformed block (see above). When I ran the program again, there was about 50 lines referenceing java.io.FileNotFoundException and java.io.FileInputStream.
Sadly, I do not know enough to make use of this information, and correct my problem.
So I still do not understand how to get the selected file played by the bPlayActionPerfomred block.
The code after line 29 in the sample above I put in only to help me understand what is being returned by FileChooser. It is not needed for the program to play a osund file so I removed it.
I noticed that I had omitted the else { statement after line 26 (above), so I added that. Here is the revised fileChooserActionPerfomred code:
Thanks for your help.Java Code:private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) { // get file name and path if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File fi1 = fileChooser.getSelectedFile(); } else { System.out.println("Operation cancelled by user."); } }
- 12-05-2012, 08:02 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
Re: File Chooser and forward and Backward Slashes
Hello. I just thought of this:
"It goes out of scope a few lines after you get it.
It never gets anywhere near the bPlayActionPerformed code. "
It goes out of scope because fileChooserActionPerformed is private, and the File fi1 is declared within this private method, so that means (I think) bPlayActionPerformed doesn;t know what fi1 is. Is that correct?
- 12-06-2012, 09:46 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: File Chooser and forward and Backward Slashes
'private' has nothing to do with it.
'fi1' is a variable local to that method, and when the method exits the object is as good as gone.
You need to get that reference to the other method, either by storing it as an attribute of whatever class, or possibly by passing it to a method that will open the audio file. Not knowing the structure of your app I can't say which is better.
And this bit simply defines a File called "fi1" which, as far as Java is concerned, to be found in the current directory.Java Code:File sound1 = new File("fi1");Please do not ask for code as refusal often offends.
Similar Threads
-
File Chooser Issue
By mrsethman in forum New To JavaReplies: 1Last Post: 10-19-2012, 09:56 PM -
How to manage files in file chooser
By vaggos in forum New To JavaReplies: 4Last Post: 09-03-2012, 12:09 PM -
File chooser internationalization problem
By Astghik in forum AWT / SwingReplies: 10Last Post: 01-14-2010, 06:44 PM -
[SOLVED] File chooser selecting file from directory...?
By prabhurangan in forum AWT / SwingReplies: 12Last Post: 06-18-2008, 04:08 AM -
Using File Chooser
By shaungoater in forum New To JavaReplies: 0Last Post: 03-20-2008, 12:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks