我想在AudioRecord循环中进行一些处理,然后在录制循环中设置一些参数。但我无法启动处理线程。
在UI线程中,我启动了设置AudioRecord实例的AsyncTask:
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
setupAnalyzer();
}
}.execute();
在setupAnalyzer()方法中,我设置了一个记录器线程和一个处理线程:
private RecognizerThread recognizerThread;
private final Handler voiceProcessingHandler = new Handler();
private HandlerThread voiceProcessingHandlerThread = new HandlerThread("handler");
//the processing handler
private final Handler mainHandler = new Handler(voiceProcessingHandlerThread.getLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
//do heavy processing
recognizerThread.getNewParamtersAfterFrameProcessed(msg.arg1);
return true;
}
});
the audio recorder thread
private final class RecognizerThread extends Thread {
int value;
//this method is then called from within processing thread when done
public void getNewParamtersAfterFrameProcessed(int nread) {
value = nread;
}
@Override
public void run() {
//the audio recording runnable
}
}
...
// start the threads
recognizerThread = new RecognizerThread();
recognizerThread.start();
voiceProcessingHandlerThread.start();
但我收到错误:
java.lang.RuntimeException:执行时发生错误 doInBackground()引起:java.lang.RuntimeException:无法创建 未调用Looper.prepare()
的线程内部处理程序
如何在继续录制音频的同时在新线程中执行一些繁重的处理,然后在处理完成后将值返回到录制器线程?