1

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

错误:

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

致命信号 11 (SIGSEGV) 位于 0x00000000 (code=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);
}

}

4

1 回答 1

1

崩溃是 pocketsphinx-android 中的一个错误。如果您从github更新到最新版本,它应该正确地抛出 RuntimeException 方法中的任何错误addKeyphrasesetSearch.

于 2015-05-17T22:56:11.900 回答