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);
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..
发布评论
评论(3)
您可以挂起其线程以实现简单的暂停功能。
You could suspend its thread for a simple pause function.
只需在线程中一次播放一帧,如果设置了“暂停”标志,则暂停线程...
如果按下 Enter 键(在控制台中)并“退出”,下面的代码将暂停/恢复播放器线程终止应用程序。
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.
我从 fileInputStream 获取 mp3。当我想暂停时,我会这样做(fis是文件输入流)fis.available()(或类似的东西)。这给出了流中剩余的字节数...如果您在开始之前执行此操作,您将获得总长度。所以总剩余量 - 当前剩余量 = 当前位置。然后我只是创建一个新的输入流并执行 fis.skip(themount);恢复..很棒的方法,但是它有效..
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..