我正在开发一个 Android 语音识别应用程序,它利用 Android 的 AudioRecord 类,除了这个缺陷之外,一切都很顺利。记录器(AudioRecord 的一个实例)在停止后无法再次重新启动,并导致 GC_CONCURRENT 跳入要求垃圾,之后程序退出。我怀疑有一些内存泄漏,但我无法理解它。
下面是我的代码:
package edu.cmu.pocketsphinx.demo;
import static edu.cmu.pocketsphinx.SphinxUtil.syncAssets;
import static edu.cmu.pocketsphinx.sphinxbase.setLogFile;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
import edu.cmu.pocketsphinx.Config;
import edu.cmu.pocketsphinx.Decoder;
import edu.cmu.pocketsphinx.Hypothesis;
public class PocketSphinxAndroidDemo extends Activity {
private class RecognitionTask
extends AsyncTask<AudioRecord, Void, Hypothesis> {
private final Decoder decoder;
public RecognitionTask() {
File root = null;
try {
root = syncAssets(getApplicationContext(), "models");
} catch (IOException e) {
throw new RuntimeException(e);
}
File rootLog = new File(root.getParentFile(), "pocketsphinx.log");
setLogFile(rootLog.getPath());
Config config = Decoder.defaultConfig();
config.setString("-lm", new File(root, "lm/hub4.5000.DMP").getPath());
config.setString("-hmm", new File(root, "hmm/hub4wsj_sc_8k").getPath());
config.setString("-dict",new File(root, "lm/hub4.5000.dic").getPath());
config.setString("-rawlogdir", root.getPath());
config.setString("-rawlogdir", root.getPath());
config.setFloat("-samprate", SAMPLE_RATE);
config.setInt("-maxhmmpf", 10000);
config.setBoolean("-backtrace", true);
config.setBoolean("-bestpath", false);
config.setBoolean("-remove_noise", false);
decoder = new Decoder(config);
}
protected Hypothesis doInBackground(AudioRecord... recorder) {
int nread;
short[] buf = new short[1024];
decoder.startUtt(null);
while ((nread = recorder[0].read(buf, 0, buf.length)) > 0){
decoder.processRaw(buf, nread, false, false);
}
decoder.endUtt();
return decoder.hyp();
}
protected void onPostExecute(Hypothesis hypothesis) {
if (null != hypothesis)
speechResult.append("\n" + hypothesis.getHypstr());
else
speechResult.append("\n<no speech>");
}
}
private static final int SAMPLE_RATE = 8000;
private static final String TAG="PocketSphinxAndroidDemo";
static {
System.loadLibrary("pocketsphinx_jni");
}
private TextView speechResult;
private AudioRecord recorder;
private RecognitionTask recTask;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speechResult = (TextView) findViewById(R.id.SpeechResult);
recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION,
SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, 204800);
recTask = new RecognitionTask();
}
public void onToggleRecognition(View view) {
Log.i(TAG, "I in ToggleRecognition");
if (!(view instanceof ToggleButton))
return;
if (((ToggleButton) view).isChecked()) {
recorder.startRecording();
recTask.execute(recorder);
} else {
recorder.stop();
}
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("OnDestroy");
recorder.release();
}
}