Handling Errors in PocketSphinx Android app

限于喜欢 提交于 2020-01-14 04:16:27

问题


I am using the default dictionary that comes with the pocketsphinx demo which is good for my purposes. When a user enters a phrase, the app starts a keyphrase listening but if the word is not found in the dictionary the app crashes. The app crashes onError() within a service. How is the error handling done? is there any way I can catch the error? Overall I would just like the service to call stopSelf() when an error happens so the main activity won't crash as well.

Errors:

ERROR: "kws_search.c", line 165: The word 'phonez' is missing in the dictionary

Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 5389 (1994.wherephone)

Here is my service class:

 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:


Crash is a bug in pocketsphinx-android. If you update to latest version from github, it should properly throw RuntimeException on any errors in methods addKeyphrase and setSearch.



来源:https://stackoverflow.com/questions/30283242/handling-errors-in-pocketsphinx-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!