该代码在所有运行 4.0 - 4.3 的 Android 设备上运行良好。但是在运行 Android 4.4 及更高版本的设备上,KWS_Search 成功启动,但应用程序在我说“激活语音”的那一刻崩溃,并出现以下错误。
02-06 20:13:40.228: I/cmusphinx(29758): INFO: fsg_search.c(843): 74 frames, 477 HMMs (6/fr), 2534 senones (34/fr), 23 history entries (0/fr)
02-06 20:13:40.235: I/SpeechRecognizer(29758): Stop recognition
02-06 20:13:40.235: I/SpeechRecognizer(29758): Start recognition "wakeup"
02-06 20:13:40.244: D/bt(29758): subclass
02-06 20:13:40.256: I/cmusphinx(29758): INFO: pocketsphinx.c(863): Writing raw audio log file: /storage/emulated/0/Android/data/com.BuildingBlocks.codigo/files/sync/000000007.raw
02-06 20:13:40.459: A/libc(29758): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x89280000 in tid 30022 (Thread-1944)
如果我使用非常高分辨率的图形,通常会在设备内存不足时收到致命信号 11
我搜索了互联网,发现谷歌在 Android 4.4 中引入了 HotKeywords,手机一直在听声音。有时,如果我在说出 KEYPHRASE 之前等待 60 秒,它会起作用。有时它不起作用,我的应用程序崩溃。下面是我的完整代码
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
// Prepare the data for UI
captions = new HashMap<String, Integer>();
captions.put(KWS_SEARCH, R.string.kws_caption);
//captions.put(MENU_SEARCH, R.string.menu_caption);
captions.put(DIGITS_SEARCH, R.string.digits_caption);
//captions.put(FORECAST_SEARCH, R.string.forecast_caption);
setContentView(R.layout.sphinx_voice);
((TextView) findViewById(R.id.speech))
.setText("Preparing the recognizer");
((TextView) findViewById(R.id.speech)).setTextSize(23);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.result_text)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.INVISIBLE);
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(SphinxVoice.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
load=2;
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if (load==2)
{
if (result != null) {
((TextView) findViewById(R.id.speech))
.setText("Failed to init recognizer " + result);
((TextView) findViewById(R.id.speech)).setTextSize(23);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.result_text)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.INVISIBLE);
} else {
switchSearch(KWS_SEARCH);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.result_text)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.INVISIBLE);
}
}
}
}.execute();
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
String text = hypothesis.getHypstr();
if (text.contains("left"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DLXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Left");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("right"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DRXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Right");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("forward"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DFXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Forward");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("back"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DBXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Back");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("stop"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DYXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Stop");
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
}else if (text.contains("deactivate"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DYXXXXXXXXXXXXXXX");
switchSearch(KWS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.INVISIBLE);
}else if (text.equals(KEYPHRASE))
{
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
}
else{
((TextView) findViewById(R.id.result_text)).setText(text);
}
}
@Override
public void onResult(Hypothesis hypothesis) {
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null) {
String text = hypothesis.getHypstr();
if (text.contains("left"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DLXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Left");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("right"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DRXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Right");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("forward"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DFXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Forward");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("back"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DBXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Executing Back");
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
}else if (text.contains("stop"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DYXXXXXXXXXXXXXXX");
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.result_text)).setText("Stop");
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
}else if (text.contains("deactivate"))
{
((CodigoApplication)SphinxVoice.this.getApplicationContext()).sendMessage("DYXXXXXXXXXXXXXXX");
switchSearch(KWS_SEARCH);
((TextView) findViewById(R.id.result_text)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.INVISIBLE);
}else if (text.equals(KEYPHRASE))
{
switchSearch(DIGITS_SEARCH);
((TextView) findViewById(R.id.deactivate)).setVisibility(View.VISIBLE);
((TextView) findViewById(R.id.listening)).setVisibility(View.VISIBLE);
}
else{
((TextView) findViewById(R.id.result_text)).setText(text);
}
}
}
@Override
public void onBeginningOfSpeech() {
((TextView) findViewById(R.id.listening)).setText("Listening...");
}
@Override
public void onEndOfSpeech() {
}
private void switchSearch(String searchName) {
recognizer.stop();
recognizer.startListening(searchName);
String caption = getResources().getString(captions.get(searchName));
((TextView) findViewById(R.id.speech)).setText(caption);
((TextView) findViewById(R.id.speech)).setTextSize(23);
}
private void setupRecognizer(File assetsDir) {
File modelsDir = new File(assetsDir, "models");
recognizer = defaultSetup()
.setAcousticModel(new File(modelsDir, "hmm/en-us-semi"))
.setDictionary(new File(modelsDir, "dict/cmu07a.dic"))
.setRawLogDir(assetsDir).setKeywordThreshold(1f-80f)
.getRecognizer();
recognizer.addListener(this);
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
// Create grammar-based searches.
Log.d("voicelog",modelsDir.getAbsolutePath());
File robbyGrammar = new File(modelsDir, "grammar/robby.gram");
Log.d("voicelog",robbyGrammar.getAbsolutePath());
recognizer.addGrammarSearch(DIGITS_SEARCH, robbyGrammar);
}
@Override
public void onBackPressed() {
recognizer.cancel();
SphinxVoice.this.finish();
}
}
下面是 robby.gram 文件
#JSGF V1.0;
语法抢劫;
= 停用语音| 离开 | 对 | 转发 | 返回 | 停止 ;