处理PocketSphinx Android应用程序中的错误 [英] Handling Errors in PocketSphinx Android app

查看:268
本文介绍了处理PocketSphinx Android应用程序中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

错误:

错误:kws_search.c,第165行:字典中缺少phonez字样

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

致命信号11(SIGSEGV)在0x00000000(code = 1),线程5389(1994.wherephone)

Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 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);
}

}

推荐答案

崩溃是pocketphinx-android中的一个错误。如果您从 github 更新到最新版本,则应正确地将RuntimeException抛出方法中的任何错误 addKeyphrase setSearch。

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.

这篇关于处理PocketSphinx Android应用程序中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆