Stop/mute playing music using JLayer

temelm picture temelm · Mar 13, 2011 · Viewed 8.1k times · Source

i am using jlayer to play mp3 files in my progam but in the jlayer documentation i could not find any useful info about stopping the playing music and continuing from where it was stopped. any ideas?

my program is as follows:


package sounds;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;

/**
 * Wayne, K. (2005). How to Play an MP3 File in Java. 
 * Available: http://introcs.cs.princeton.edu/faq/mp3/mp3.html.
 * Last accessed 10th Mar 2011.
 * @author temelm
 */
public class MP3 {
  private String filename;
  private Player player;

  /**
   * MP3 constructor
   * @param filename name of input file
   */
  public MP3(String filename) {
    this.filename = filename;
  }

  /**
   * Creates a new Player
   */
  public void play() {
    try {
      FileInputStream fis = new FileInputStream(this.filename);
      BufferedInputStream bis = new BufferedInputStream(fis);

      this.player = new Player(bis);
    } catch (Exception e) {
        System.err.printf("%s\n", e.getMessage());
    }

    new Thread() {
      @Override
      public void run() {
        try {
          player.play();
        } catch (Exception e) {
            System.err.printf("%s\n", e.getMessage());
        }
      }
    }.start();
  }

  /**
   * Closes the Player
   */
  public void close() {
    if (this.player != null) {
      this.player.close();
    }
  }

  /////////////////////////

  /**
   * Plays '01 Maenam.mp3' in an infinite loop
   */
  public static void playMaenam() {
    MP3 mp3 = new MP3("./01 Maenam.mp3");

    mp3.play();

    while (true) {
      if (mp3.player.isComplete()) {
        mp3.close();
        mp3.play();
      }
    }
  }
}

Answer

Jack picture Jack · Jun 26, 2012

You could suspend its thread for a simple pause function.