title: how to automatically start new song after old one ends
body: <p>I am making an MP3 player but my problem is I cant automatically start a new song after the old one is finished. I have tried using isComplete()</p>
  
  <p>Then I tried finding the length of the song and using thread.sleep() and I set it as the same length as my song so when the song is over a new one starts and it worked, the only problem is I cant click on any buttons and when i try to exit nothing happens, the song keeps playing and a new one will start but the window freezes</p>
  
  <p>If you could help that would be great (The problem is around line 312)</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
  import java.util.*;
  import java.util.concurrent.*;
  import java.util.logging.*;
  import java.lang.reflect.Array;
  import java.lang.reflect.Method;
  import java.net.*;
  
  import javax.sound.sampled.AudioFileFormat;
  import javax.sound.sampled.UnsupportedAudioFileException;
  import javax.swing.*;
  
  import javazoom.jl.player.Player;
  import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
  
  
  public class PPR extends JPanel{
  
  private static long startTime;
  private static long stopTime;
  static int ms;
  
  static int songnumber = 0;
  static String song;
  static Long duration;
  //Above is the song number and the path
  static File test = new File ("C:/Users/cmtc/Documents/WorkSpace/Mp3 Player/music/aa.mp3");
  
  static File[] files = new File("music").listFiles();
  static LinkedList songnames = new LinkedList();
  //above is the array that gets all of the files and the linked list that gets all the names
  
  
  
   private final static int NOTSTARTED = 0;
   private final static int PLAYING = 1;
   private final static int PAUSED = 2;
   private final static int FINISHED = 3;
      //above tell the player what is happening
  
  
      private final Object playerLock = new Object();
      //not sure if i need above
  
  
      private static int playerStatus = NOTSTARTED;
      //above starts off by saying the player hasnt started yet
  
  
  static JFrame f = new JFrame();
  static JPanel p = new JPanel();
  static String filename = "this string doesnt matter";
  
  
  //above is the jpanel and jframe
  
   static FileInputStream input; 
   static MP3 player; 
   //above allows for file io and the mp3 player
  
      static Runnable Graphic = new Runnable() {
       public void run() {
           gui();
       }
   };
  
   // I had to make threads so that more than one button is visible
  
   public static void invokeLater(Runnable doRun){
       SwingUtilities.invokeLater(Graphic);
  }
  
   //above is what allows multiple buttons to be visible
  
  
  
  
  public PPR(){
  
      SwingUtilities.invokeLater(Graphic);
  }
  
  //above is similar to what is above this
  
  
  public static void gui(){
      f.setVisible(true);
          f.setSize(1600,900);
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  f.add(p);
  
      PlayPauseRewind();
  
  }
  
  //above is baseically what runs the three buttons
  
  public static void PlayPauseRewind(){
  
  
  
  
      Object play = ppr(80,26,25,25,"pics/play.jpg");
  
      ((AbstractButton) play).addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
              startTime = e.getWhen();
              if (playerStatus == PLAYING){
                  pause();
                  playerStatus = PAUSED;
              }else if (playerStatus == NOTSTARTED){
                   play();
                   playerStatus = PLAYING;
              }else if (playerStatus == PAUSED){
               resume();
               playerStatus = PLAYING;
          }
  
  
          }
  
      });
  
  
  
      //above is play ***************************************
  
  
      Object next = ppr(120,26,25,25,"pics/next.png");
      ((AbstractButton) next).addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
              if (songnumber == files.length-1){
                  System.out.println("No More songs");
              }else if (playerStatus == NOTSTARTED){
                  play();
                  playerStatus = PLAYING;
                  pause();
                   playerStatus = PAUSED;
                   songnumber++;
                   play();
                      playerStatus = PLAYING;
                  System.out.println(songnumber);
              }else if (playerStatus == PAUSED){
                  songnumber++;
                  play();
                  pause();
                  playerStatus = PAUSED;
                  System.out.println(songnumber);
              }else{
              stop();
              songnumber++;
              System.out.println(songnumber);
  
              //above tells the next button what to do below is the try/catch
  
  
              try {
                  input = new FileInputStream(allsongs(songnumber,song)); 
                  player = new MP3(input);
  
                  // start playing
                  player.play();
  
                  // after 5 secs, pause
  
              } catch (final Exception e1) {
                  throw new RuntimeException(e1);
              }
              playerStatus = PLAYING;
              }   
          }
      });
  
  
  
      Object back = ppr(40,26,25,25,"pics/back.png");
      ((AbstractButton) back).addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
              if (songnumber == 0){
                  System.out.println("No More songs");
              }else if (playerStatus == NOTSTARTED){
                  play();
                  playerStatus = PLAYING;
                  pause();
                   playerStatus = PAUSED;
                   songnumber--;
                   play();
                      playerStatus = PLAYING;
                  System.out.println(songnumber);
              }else if (playerStatus == PAUSED){
                  songnumber--;
                  play();
                  pause();
                  playerStatus = PAUSED;
                  System.out.println(songnumber);
              }else{
              stop();
              songnumber--;
              System.out.println(songnumber);
  
              //above tells the back button what to do below is the try/catch
  
              try {
                  input = new FileInputStream(allsongs(songnumber,song)); 
                  player = new MP3(input);
  
                  // start playing
                  player.play();
  
                  // after 5 secs, pause
  
              } catch (final Exception e1) {
                  throw new RuntimeException(e1);
              }
              playerStatus = PLAYING;
              }   
          }
      });
  
  }
  
  public static Object ppr(int x, int y, int width, int height, String file){
      p.setLayout(null);      
  
      Toolkit tool = Toolkit.getDefaultToolkit();
          Image player = tool.getImage(file);
              ImageIcon playbutton = new ImageIcon(player);
  
      JButton play = new JButton(playbutton);
          play.setBounds(x, y, width, height);
  
              return p.add(play);
  
              //above is a method that makes creating buttons very easy
  }
  
  
  public static void play(){
      startTime = System.currentTimeMillis();
      try {
          input = new FileInputStream(allsongs(songnumber,song)); 
          player = new MP3(input);
  
          player.play();
  
      } catch (final Exception e) {
          throw new RuntimeException(e);
      }
      playerStatus = PLAYING;
  
      nextsong();
  
  
  }
  
  //above is the play method
  
  
  
  
  public static void pause(){
      try {
  
          // start playing
          player.pause();
  
          // after 5 secs, pause
  
      } catch (final Exception e) {
          throw new RuntimeException(e);
      }
      playerStatus = PAUSED;
  }
  //above is the pause method
  
  public static void resume(){
      try {
  
          // start playing
          player.resume();
  
          // after 5 secs, pause
  
      } catch (final Exception e) {
          throw new RuntimeException(e);
      }
  }
  
  public static void length(){
  
      long size = files[songnumber].length();
      AudioFileFormat baseFileFormat;
      int ms = 0;
      try {
          baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(files[songnumber]);
          Map properties = baseFileFormat.properties();
          duration = (Long) properties.get("duration");
          duration = duration/1000;
          ms = (int) (duration/10);
          System.out.println(ms);
          //System.out.println(size);
      } catch (UnsupportedAudioFileException | IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  //ABOVE AND BELOW IS WHERE I AM TRYING TO FIX IT;
  
  public static void nextsong(){
      length(); 
      try {
          Thread.sleep(duration);
          stop();
          songnumber++;
          play();
          playerStatus=PLAYING;
  
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  
  public static void stop(){
      try {
  
          player.stop();
  
      } catch (final Exception e) {
          throw new RuntimeException(e);
      }
  }
  
  //above is the resume method
  
  public static void temp(){
      JOptionPane.showMessageDialog(null, "Action Listener is working");
  }
  //above is what I use to make sure the actionlistener is working I wont need it eventually
  
  public static String allsongs(int songnumber,String song){
              for (int i = 0; i&lt;files.length;i++){
  
              songnames.add("music/" + files[i].getName());   
  
              song = (String) songnames.get(songnumber);
  
          }
              return song;
      }
  
  //above is the loop that goes through the folder and names everysong while         assigning it a song number
  
  
  }
  </code></pre>