Reputation: 99
I'm trying to play a song from a folder that a user selects. Essentially, I am using my own Queue that I've created and I'm getting the right path.
Within the code below, I am using a Var called path. The path is "C:\Users\Shaun\Downloads\TestMusic\Ed Sheeran - Shape of You.mp3". When I define the path as just, "Ed Sheeran - Shape of You.mp3". It works! This tells me that this looks into the directory of where the project is started or runned from.
So, how do I make it play a file from any given directory?
The 'path' I'm referring to is below, " public void handlecentreButtonClick()".
public class graphicalController implements Initializable
{
//GUI Decleration
public Button centreButton;
public Button backButton;
public Button forwardButton;
public ToggleButton muteToggle;
public MenuItem loadFolder;
//Controller Decleration
String absolutePath;
SongQueue q = new SongQueue();
MediaPlayer player;
@Override
public void initialize(URL location, ResourceBundle resources)
{
centreButton.setStyle("-fx-background-image: url('/Resources/Play_Button.png')");
centreButton.setText("");
backButton.setStyle("-fx-background-image: url('/Resources/Back_Button.png')");
backButton.setText("");
forwardButton.setStyle("-fx-background-image: url('/Resources/Forward_Button.png')");
forwardButton.setText("");
muteToggle.setStyle("-fx-background-image: url('/Resources/ToggleSound_Button.png')");
muteToggle.setText("");
}
public void handlecentreButtonClick() {
if(!(q.isEmpty())) {
String file = q.peek().fileName.toString();
String path = absolutePath + "\\" + file;
Media song = new Media(path);
player = new MediaPlayer(song);
player.play();
}
}
public void handleforwardButtonClick() {
System.out.println("Hello.");
centreButton.setText("Hello");
}
public void handlebackButtonClick() {
System.out.println("Hello.");
centreButton.setText("Hello");
}
public void handleLoadButtonClick() {
DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(null);
absolutePath = selectedDirectory.getAbsolutePath();
String path = absolutePath;
loadFilesFromFolder(path);
}
public void loadFilesFromFolder(String path) {
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
while(!(q.isEmpty()))
{
try {Thread.sleep(500);}catch (Exception e){}
Song j = q.pop();
}
int listLength = listOfFiles.length;
for (int k = 0; k < listLength; k++) {
if (listOfFiles[k].isFile()) {
String fileName = listOfFiles[k].getName();
String fileNamePath = path + "\\" +fileName;
try {
InputStream input = new FileInputStream(new File(fileNamePath));
ContentHandler handler = new DefaultHandler();
Metadata metadata = new Metadata();
Parser parser = new Mp3Parser();
ParseContext parseCtx = new ParseContext();
parser.parse(input, handler, metadata, parseCtx);
input.close();
String songName = metadata.get("title");
String artistName = metadata.get("xmpDM:artist");
String albumName = metadata.get("xmpDM:genre");
int id = k + 1;
Song newSong = new Song(id, fileName, songName, artistName, albumName);
q.push(newSong);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
}
}
}
}
}
Upvotes: 0
Views: 243
Reputation: 209358
Use
Media song = new Media(new File(path).toURI().toString());
I strongly recommend you construct the file in a platform independent way, however, instead of hard-coding a file separator specific to one particular file system. You can do
File path = new File(absolutePath, file);
Media song = new Media(path.toURI().toString());
Upvotes: 1
Reputation: 99
With the help of @James_D...
You Cannot:
Media song = new Media("C:\Users\Shaun\Downloads\TestMusic\Ed Sheeran - Shape of You.mp3");
This will try and find the directory from the point of where you've launched your program.
Do:
Media song = new Media(new File(path).toURI().toString());
Upvotes: 0