How to make an MP3 file wait to play until after another MP3

352 Views Asked by At

I am working on a program that will play playlists consisting of MP3s.

I am using the following MP3 Class

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javazoom.jl.player.Player;
public class MP3 {
private Player player;
public BufferedInputStream bis;
// constructor 
public MP3() {
}
public void close() throws IOException {
    if (player != null)
        player.close();
}
// play the MP3 file to the sound card
public void play(String filename) {
    try {
        InputStream stream = MP3.class.getClassLoader()
                .getResourceAsStream(filename);

        bis = new BufferedInputStream(stream);
        player = new Player(bis);

    } catch (Exception e) {
        System.out.println("Problem playing file " + filename);
        System.out.println(e);
    }

    // run in new thread to play in background
    new Thread() {
        public void run() {
            try {
                player.play();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }.start();

}
}

I have been able to play MP3 files based on this code but I am having trouble playing multiple files. When I first tried to play an ArrayList of MP3s all of the songs played right over each other. I then tried to use the .wait method to make the second song wait until the first song was done playing. This worked however, this also made everything in my program wait until the song was done playing before anything else could execute. I want my program to be able to do multiple things while a song is playing so that method won't work.

This is the code I currently have that uses .wait

} else if (e.getSource() == btn2) {
            ArrayList<Integer> songsToPlay = new ArrayList<Integer>();
            songsToPlay.add(1);
            songsToPlay.add(2);
            if (b2 == false) {
                b2 = true;
                mp3 = new MP3();
            //loop through the ArrayList of songs 
            for (int i = 0; i < songsToPlay.size(); i++) {
                mp3.play("music/" + songsToPlay.get(i) + ".mp3");
                synchronized (mp3) {
                    try {
                        mp3.wait(1000);
                    } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                   // e2.printStackTrace();
            }
            }
            }
            } else {
                  b2 = false;
                  try {
                      mp3.close();
                  } catch (IOException e1) {
                  // TODO Auto-generated catch block
                 // e1.printStackTrace();
              }
          }

        }

Any advice would be great as I have been stuck on this for awhile. Also if I were to figure out a way to use .wait I would also need a way to calculate the length of the current MP3. Right now I am just hardcoding in values.

1

There are 1 best solutions below

1
On

Is there a reason why you are trying to accomplish this all manually? Why not use the java VLC library as a player for example?

On the topic, you seem to be missing some valuable information to build a convenient player. What's the length of the current track? How will the user seek through a song? Do you read the mp3 file's ID3 tag to display information like title, artist, album?

I know this is not a direct answer to the problem but it might help you find it on your own.