0

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!

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
april21
  • 115
  • 2
  • 9
  • Your playlist should be a List of Songs or path to the song files. On click of the next button, they should load the next item in the Media, set it into the MediaPlayer and play it via the MediaPlayer. You can have a look at [this project](https://github.com/TheItachiUchiha/MediaPlayerFX) and especially [this class](https://github.com/TheItachiUchiha/MediaPlayerFX/blob/master/src/main/java/com/ita/controller/PlaylistController.java) – ItachiUchiha May 21 '15 at 19:47
  • "I've tried a few combinations but all of them are throwing errors!" Can you tell us what the errors you're getting are? – javaauthority May 21 '15 at 19:48
  • But it doesn't seem relevant? Do you think there is an easier way to do it? – april21 May 21 '15 at 20:00
  • @javaauthority, i've fixed them but now it only reads the one file fed into it. Would you be able to help me out please? – april21 May 21 '15 at 20:01
  • @april21 the part you're having issues with is the playlist class? Populating the ArrayList? Or is it in integrating the playlist class within the media player class? And, again, can you show us what errors are thrown? – javaauthority May 21 '15 at 20:05
  • no errors are being thrown anymore! It's just not coming as a playlist. Yup could you please help me with the ArrayList population using music files? Then I'll try to do the rest :) It's just that I have spent ages and I don't get it – april21 May 21 '15 at 20:07
  • @april21 Do you want to choose all songs from a directory and add them in your playlist (arraylist of files)? – ItachiUchiha May 21 '15 at 20:11
  • yes, that's it. If I create a method with iterates through the folder and adds all the files into an array list, and then pass that through the music player. it should work right? – april21 May 21 '15 at 20:19
  • But I don't understand how to create the arraylist of files :/ – april21 May 21 '15 at 20:20
  • @ItachiUchiha, also thank you for phrasing my question better. I'll certainly ensure improve that for next time :) hope you can help me out with this – april21 May 21 '15 at 20:23
  • Please help me out! @javaauthority – april21 May 21 '15 at 20:41
  • please guide me @ItachiUchiha!! I'd really appreciate your help – april21 May 21 '15 at 20:53
  • [Source for the sample app](https://gist.github.com/jewelsea/1446612) for the answer to the potential duplicate. – jewelsea May 21 '15 at 21:12
  • @jewelsea I don't understand why you are using such a long code for functions like mediaPlayer.pause(); Could you please help me understand the array of files bit? – april21 May 21 '15 at 21:17
  • 1
    @april21 Sorry I can't really offer good a good explanation of all the sample code within the StackOverflow format. I suggest you ask a new question which is far more specific - as the current question is too broad. For example, ask a new question "How do I use a java.io.FilenameFilter to retrieve a list of .mp3 filenames from a directory?". Such a question is not JavaFX specific and you will get many answers to it very quickly. Similarly, ask other questions for other specific issues you have. – jewelsea May 21 '15 at 23:27
  • @jewelsea, sure, I have asked a new question as you advised :) thanks for your suggestion! I will certainly follow that – april21 May 23 '15 at 14:12

0 Answers0