Reputation: 31
I'm coding a small audio player and need help here; the method fopen() is called by a button press in another class (not the issue here); the problem is that I cannot get the file's path as a string without calling the method.
The playsound() method needs the filepath variable from fopen(), and if I use the String 'path' (initialized after fopen()) it calls the method again.
I ONLY need the 'filepath' variable, but I cannot access it outside of fopen(), or at least not that I know of. Assistance on how I can access filepath without invoking fopen()?
EDIT: Fixed fopen() being set up to return a 'File' instead of a string. Also made some changes to the code; the issue of having fopen() called when it's not supposed to be is fixed, but now it gives me a java.io.FileNotFoundException: when I call playsound() (which, from what I understand, means that the file's path and/or name wasn't even recorded). What else is going on here?
Edit 2: I'm just going to ask another question, seeing as the problem at hand seems to have been answered, and I have an entirely different one on my hands.
package mediaz;
import javazoom.jl.player.*;
import javax.swing.filechooser.*;
import java.io.*;
import javax.swing.JFileChooser;
public class audio {
private String lastfilepath = "";
public String fopen(){
JFileChooser fc= new JFileChooser();
FileNameExtensionFilter filtermp3 = new FileNameExtensionFilter("MPEG-2
Audio Layer III", "mp3");
fc.setFileFilter(filtermp3);
int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String filepath = file.getAbsolutePath();
this.lastfilepath = filepath;
return filepath;
}
else
return null;
}
String path = fopen();
void playsound(){
System.out.println("You pressed play.");
try{
FileInputStream fis = new FileInputStream(this.lastfilepath);
Player playMP3 = new Player(fis);
playMP3.play();
}
catch(Exception e){
System.out.println("Error: '" + e +"'");
}
}
// IGNORE WHAT'S BELOW HERE //
void rewsound(){
System.out.println("You pressed rewind.");
}
void pausesound(){
System.out.println("You pressed pause.");
}
/* void forwardsound(){
System.out.println("You pressed fast forward.");
}
*/
}
Upvotes: 0
Views: 105
Reputation: 630
Create a String instance variable in audio, and then when you call fopen() store the currently selected file's path in that string.
See code below. Untested, but the idea is here. Also, the formatting of this code is pretty bad, it's hard to read. This is what it should look like (ish).
Edit: Added some comments in the code on general improvements/coding style
Edit: For more info on the try I updated in the code, see: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
package mediaz;
import javazoom.jl.player.*;
import javax.swing.filechooser.*;
import java.io.*;
import javax.swing.JFileChooser;
public class audio {
private String filePath = "";
public File fopen() {
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filtermp3 = new FileNameExtensionFilter("MPEG-2
Audio Layer III ", "
mp3 ");
fc.setFileFilter(filtermp3); int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.filePath = file.getAbsolutePath()
return filepath; // should be file
} else // give me braces please!
return null;
}
// try to stick to camelCase, it is the 'Java' way
void playsound() {
System.out.println("You pressed play.");
// streams implement AutoCloseable, use it
// also, you were not closing fis as it was
try (FileInputStream fis = new FileInputStream(this.filePath)) {
Player playMP3 = new Player(fis);
playMP3.play();
} catch (Exception e) {
System.out.println("Error: '" + e + "'");
}
}
}
Upvotes: 2
Reputation: 29
First of all you should read about scopes in java programming.
What you currently have is a local scope for your variable "filepath". To make it accessible outside its method block you can either return it as the method result or asign it to a instance variable.
In addition please note that your fopen() method currently won't compile cause it is declared to return a File but inside tries to return a String type.
I would recommend the following:
public class foo {
private String filePath;
private void readFile() {
filePath = doReadingHere();
}
private void useFilePath() {
System.out.println(filePath);
// do what ever you like to do with the instance variable filePath
}
}
Upvotes: 0
Reputation: 425043
Create another method that returns the last filepath determined in fopen()
, eg:
private String lastFilepath;
public String fopen() {
// logic for determining filepath
lastFilepath = filepath;
return filepath;
}
public String getLastFilepath() {
return lastFilepath;
}
Upvotes: 1
Reputation: 11832
Your fopen()
method is declared to return a File
, yet in the method you return a String. If you returned the file that the user selected, and then stored this reference somewhere, you could ask that file for its path any time you wanted.
Upvotes: 2