处理PocketSphinx Android应用程序中的错误

时间:2015-05-17 04:03:06

标签: android error-handling cmusphinx pocketsphinx pocketsphinx-android

我正在使用pocketphinx演示附带的默认字典,这对我的目的很有用。当用户输入短语时,应用程序会启动一个关键短语,但如果在字典中找不到该单词,则应用程序会崩溃。该应用程序崩溃服务中的onError()。如何完成错误处理?有什么方法可以捕捉错误吗?总的来说,我希望服务在发生错误时调用stopSelf(),这样主要活动也不会崩溃。

错误:

错误:“kws_search.c”,第165行:字典中缺少“phonez”一词

致命信号11(SIGSEGV)位于0x00000000(代码= 1),线程5389(1994.wherephone)

这是我的服务类:

 public class WherePhoneService extends Service implements RecognitionListener {

private static String SettingStorage = "SavedData";
SharedPreferences settingData;

private SpeechRecognizer recognizer;

private String sInput;
private String sOutput;

private int seekVal;
private TextToSpeech reply;

private AsyncTask t;


public WherePhoneService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    makeText(getApplicationContext(), "onHandle start", Toast.LENGTH_SHORT).show();

    getValues();

    startTTS();

    t = new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(WherePhoneService.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                //((TextView) findViewById(R.id.caption_text)).setText("Failed to init recognizer " + result);
            } else {
                switchSearch(sInput);
            }
        }
    }.execute();
    return Service.START_STICKY;
}

private void setupRecognizer(File assetsDir) throws IOException {
        recognizer = defaultSetup()
                .setAcousticModel(new File(assetsDir, "en-us-ptm"))
                .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))

                        // To disable logging of raw audio comment out this call (takes a lot of space on the device)
                        //.setRawLogDir(assetsDir)

                        // Threshold to tune for keyphrase to balance between false alarms and misses
                .setKeywordThreshold(1e-45f)

                        // Use context-independent phonetic search, context-dependent is too slow for mobile
                .setBoolean("-allphone_ci", true)

                .getRecognizer();
        recognizer.addListener(this);

        // Create keyword-activation search.
        recognizer.addKeyphraseSearch(sInput, sInput);
}

private void switchSearch(String searchName) {
    recognizer.stop();

    // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
    if (searchName.equals(sInput))
        recognizer.startListening(searchName);
    else
        recognizer.startListening(searchName, 10000);
}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().equals(sInput))
        switchSearch(sInput);
}

@Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
    makeText(getApplicationContext(), "Partial", Toast.LENGTH_SHORT).show();

    if (text.equals(sInput)) {

        setVolume();

        // Text to speech
        reply.speak(sOutput, TextToSpeech.QUEUE_ADD, null);

        switchSearch(sInput);
    }
    else {
        makeText(getApplicationContext(), "Try again", Toast.LENGTH_SHORT).show();
        switchSearch(sInput);
    }
}

@Override
public void onResult(Hypothesis hypothesis) {
    if (hypothesis != null) {
        // restart listener and affirm that partial has past
        makeText(getApplicationContext(), "end", Toast.LENGTH_SHORT).show();
        //recognizer.startListening(sInput);
        switchSearch(sInput);
    }
}


public void onError(Exception e) {
    e.printStackTrace(); // not all Android versions will print the stack trace automatically

    Intent intent = new Intent ();
    intent.setAction ("com.mydomain.SEND_LOG"); // see step 5.
    intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
    startActivity (intent);

    stopSelf();
}

@Override
public void onTimeout() {
    switchSearch(sInput);
}

public void startTTS() {
    reply = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR){
                reply.setLanguage(Locale.UK);
            }
        }
    });
}

public void getValues() {
    settingData = getBaseContext().getSharedPreferences(SettingStorage, 0);
    sInput = settingData.getString("inputstring", "Where is my phone").toString().toLowerCase().replaceAll("[^\\w\\s]", "");
    sOutput = settingData.getString("outputstring", "").toString().toLowerCase();
    seekVal = settingData.getInt("seekval", 0);
}

public void setVolume() {
    int seekValConvert = 0;
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int getMaxPhoneVol = audioManager.getStreamMaxVolume(audioManager.STREAM_MUSIC);
    seekValConvert = ((seekVal * getMaxPhoneVol)/100);
    audioManager.setStreamVolume(audioManager.STREAM_MUSIC, seekValConvert, 0);
}

@Override
public void onDestroy() {
    super.onDestroy();
    makeText(getApplicationContext(), "destroy", Toast.LENGTH_SHORT).show();
    recognizer.cancel();
    recognizer.shutdown();
    t.cancel(true);
}

}

1 个答案:

答案 0 :(得分:1)

Crash是pocketsphinx-android中的一个错误。如果您从github更新到最新版本,则应该对方法addKeyphrasesetSearch.

中的任何错误正确抛出RuntimeException