我正在開發一個Android語音識別應用程序,它利用了Android的AudioRecord類,除了這個缺陷外,一切都很順利。記錄器(AudioRecord的一個實例)在停止後不能再次重新啓動,並且導致GC_CONCURRENT跳入聲明垃圾程序,之後程序退出。我懷疑有一些內存泄漏,但無法擺脫它。爲什麼AudioRecord實例無法在事先停止後重新啓動
下面是我的代碼:
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();
}
}