开发者

Stop/mute playing music using JLayer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_开发技巧

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

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();
      }
    }
  }
}


You could suspend its thread for a simple pause function.


Just play one frame at a time in the thread, if the "pause" flag is set, then pause the thread...

The code below pauses/resumes the player thread if enter key is pressed (in console) and "exit" to terminate the application.

            final AtomicBoolean pause = new AtomicBoolean(false);

        final Player player = new Player(bis);
        Thread playerThread = new Thread() {
            @Override
            public void run() {
                try {
                    while (player.play(1)) {
                        if(pause.get()) {
                            LockSupport.park();
                        }
                    }
                }
                catch (Exception e) {
                    System.err.printf("%s\n", e.getMessage());
                }
            }
        };

        playerThread.start();

        Scanner scanner = new Scanner(System.in);
        String s = null;
        while (!(s = scanner.nextLine()).equals("exit")) {
            if (s.isEmpty()) {
                pause.set(!pause.get());
                if (!pause.get()) {
                    LockSupport.unpark(playerThread);
                }
            }
        }
        System.exit(0);


I get my mp3's from a fileInputStream. When I want to pause, I do (fis is the fileinputstream) fis.available() (or something similar). This gives how many bytes left in the stream... If you do this before it starts, you get the total length. So total left - amount currently left = Current position. Then i just create a new inputstream and do fis.skip(theamount); to resume.. Aweful method but yeah it works..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜