1
代碼在運行4.0 - 4.3的所有Android設備上運行良好。但是,在運行Android 4.4及更高版本的設備上,KWS_Search啓動成功,但是應用程序在我說「激活語音」的那一刻崩潰,並出現以下錯誤。Pocketsphinx android演示在Android 4.3及更低版本上運行良好。但它在Android 4.4及以上崩潰
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;
語法羅比;
=停用語音| 離開| right | 轉發| 返回| 停止;