我正在使用 jlayer 在我的程序中播放 mp3 文件,但在 jlayer 文档中,我找不到任何有关停止播放音乐并从停止位置继续播放的有用信息。有任何想法吗?
我的程序如下:
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();
}
}
}
}