在我的应用程序中,我通过按下按钮启动一个运行几个音频服务的线程,然后通过按下停止按钮尝试停止它。音频部分停止正常,但我在调试窗口中看到线程仍在运行。任何帮助将不胜感激
这是启动的线程,goTime被初始化为null作为全局变量:
goTime = new Thread(new Runnable() {
public void run() {
rec.startRecording();
player.play();
while(isRecording && !Thread.currentThread().isInterrupted()) {
if(goTime.isInterrupted()){
return;
}
ix=0;
while(ix<bufLeng){
short[] bufferor = buffers[ix++ % buffers.length];
lastRead = ix;
rec.read(bufferor, 0,
bufferor.length);
buffers[ix]=bufferor;
inter.onMarkerReached(rec);
}
}
if(goTime.isInterrupted()){
return;
}
}});
goTime.start();
以下是按下停止按钮时调用的方法:
public void stop(View view){
isRecording=false;
System.out.println("stop called");
rec.stop();
player.stop();
player.flush();
player.release();
goTime.interrupt();
//isRecording=true;
}
答案 0 :(得分:0)
有时线程之间的对象值由JVM缓存(或者在这种情况下为Dalvik) 尝试像这样声明你的isRecording:
private volatile boolean isRecording;
volatile关键字将确保不缓存值,但始终直接写入