W
W
wolf-98302015-05-13 20:14:58
Java
wolf-9830, 2015-05-13 20:14:58

How to write a normal audio player?

I have an ArrayList with paths to music files, the music class is like this:

public class Track {
  
  public Media track;
  public MediaPlayer player;
  
  public Track(){

  }
  
  public void setMusic(String musicPath){
    if(player!=null){
      player.stop();
      player.setAudioSpectrumListener(null);
      
    }else{
      track = new Media(new File(musicPath).toURI().toString());
      player = new MediaPlayer(track);
    }
  }
  
  public void playMusic(){
    player.play();
  }
  
  public void pauseMusic(){
    player.pause();
  }
  
}

But for some reason, only the song that I first clicked on is always played, i.e. I click on other songs, but the first song still plays, how can I fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2015-05-14
@jkotkot

Run the debugger and see that execution enters the else branch only once on the first click, and on other clicks it goes along the main branch ... and at the end put player=null where it is required.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question