I'm trying to create a music player. My aim is for the user to choose the play button and it to play music, pause then pause, press the Next >>
button and it should move on to the next song, and press the Previous <<
button and it should go back to the previous song.
This is my code at the moment to play the music: *this plays only one song in the folder names "Songs"
//mp3 player
String bob = getClass().getResource("Songs/Sing.m4a").toExternalForm();
Media lol = new Media(bob);
MediaPlayer myMedia = new MediaPlayer(lol);
//play button for mp3 player - in My Library scene
public void audioPlayerButtons(ActionEvent actionEvent) {
if (actionEvent.getSource() == playbtn) {
myMedia.play();
nowPlaying.setText("Now Playing...");
songPlayingName.setText("Sing - Ed Sheeran");
} else if (actionEvent.getSource() == stopbtn) {
myMedia.stop();
nowPlaying.setText("Stopped");
songPlayingName.setText("-");
} else if (actionEvent.getSource() == pausebtn) {
myMedia.pause();
nowPlaying.setText("Paused");
}
}
public void forwardAudio(ActionEvent actionEvent) {
if (actionEvent.getSource() == forwardbtn) {
//mp3 player
nowPlaying.setText("Now Playing...");
songPlayingName.setText("No Better - Lorde");
if (actionEvent.getSource()==forwardbtn) {
myMedia.pause();
if(actionEvent.getSource()==stopbtn){
myMedia.stop();
}
}
}
}
public void backAudio(ActionEvent actionEvent) {
if (actionEvent.getSource() == backwardbtn) {
//mp3 player
nowPlaying.setText("Now Playing...");
songPlayingName.setText("Dark Horse - Kate Perry");
}
}
I have made an import Music file so that it reads through the folder all the music files are in and plays them in a playlist:
public class playList {
ArrayList<String> titles = new ArrayList<String>();
public String playList() throws IOException {
FileReader fr = new FileReader("Songs");
BufferedReader br = new BufferedReader(fr);
String s;
while ((s = br.readLine()) != null) {
titles.add(s);
}
fr.close();
}
}
any idea on how to make it work? I've tried a few combinations but all of them are throwing errors!