Search code examples
javamedia-playermediafxml

Creating a music player with javafx and scene builder


I'm trying to create my own version of iTunes. I am trying to create a music player and this is my method:

public void audioPlayerButtons(ActionEvent actionEvent) {
    if (actionEvent.getSource() == playbtn) {

        String bip = "/Users/april121/Work/MyMusic!/src/sample/Songs/01 Clarity.m4a";
        Media hit = new Media(bip);
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        MediaPlayer.play();

    }

   else (actionEvent.getSource()== pausebtn){
           MediaPlayer.pause();
   }

   else (actionEvent.getSource()==forwardbtn){
       MediaPlayer.seek(MediaPlayer.getStartTime());
       MediaPlayer.stop();
   }

   else (actionEvent.getSource()==backwardbtn){
//to be finished
    }

But I have tried for hours now - be it through importing libraries from Maven or hard coding and it's not working.

I'd like it show what's playing and have basic functions ie. play, pause, rewind and forward and have a progress bar.

this is the error it is showing:

non-static method can't be accessed in static context. And the part that is causing the error is the ".stop()" or ".play()" bits

but I don't understand why - because my method is non-static anyways


Solution

  • Look at these lines:

    MediaPlayer mediaPlayer = new MediaPlayer(hit);
    MediaPlayer.play();
    

    That second line is calling a static play() function, which doesn't work. The play() function is non-static. That's why you're getting the error you're getting.

    You probably mean this instead:

    MediaPlayer mediaPlayer = new MediaPlayer(hit);
    mediaPlayer.play();
    

    If you have other questions, post them as separate questions and try to be as specific as possible.