1

几天前我做了语音识别服务,它工作得很好,但现在当我再次在手机上运行时,当我说一些东西时,它只是“onstart”和“onend”,没有任何识别,但有时它是“onstart”并听取关键字采取行动如何确保每次我说关键字时都能正确识别

public class VoiceService extends Service implements
    RecognitionListener{

private static final String LOG_TAG = VoiceService.class.getSimpleName();

private static final String KWS_SEARCH = "wakeup";
private static final String KEYPHRASE = "okay computer";
private SpeechRecognizer recognizer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
        runRecognizerSetup();
    }
    return super.onStartCommand(intent, flags, startId);
}

private void runRecognizerSetup() {
    // 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(VoiceService.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception e) {
            if (e != null) {
                Log.i(LOG_TAG, "Failed to init recognizer ");
            } else {
              //  switchSearch(KWS_SEARCH);
                startListening(KWS_SEARCH);
            }
        }
    }.execute();
}

private void startListening(String kwsSearch) {
    recognizer.startListening(KWS_SEARCH);
    Log.i(LOG_TAG, "startedlistening");
}

@Override
public void onDestroy() {
}


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

    String text = hypothesis.getHypstr();
    if (text.contains(KEYPHRASE)) {
        switchSearch(KWS_SEARCH);
    }
}

/**
 * This callback is called when we stop the recognizer.
 */
@Override
public void onResult(Hypothesis hypothesis) {
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        Intent Intent = new Intent(this, ReceiverActivity.class);
        Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent);

    }

}

@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
}


@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().contains(KWS_SEARCH))
        recognizer.stop();
        recognizer.startListening(KWS_SEARCH);
    Log.i(LOG_TAG, "onEndOfSpeech");
}

private void switchSearch(String searchName) {
    if (recognizer != null) {
        recognizer.stop();
        recognizer.startListening(searchName);

    }
}



private void setupRecognizer(File assetsDir) throws IOException {
    recognizer = SpeechRecognizerSetup.defaultSetup()

            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            .setRawLogDir(assetsDir) 
            .setKeywordThreshold(1e-45f) 
            .setBoolean("-allphone_ci", true) 
            .getRecognizer();
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
    recognizer.addListener(this);
}

@Override
public void onError(Exception error) {
    Log.i(LOG_TAG, "onError " + error.getMessage());
}

@Override
public void onTimeout() {
    switchSearch(KWS_SEARCH);
    Log.i(LOG_TAG, "onTimeout");
}
}

关键字.gram

okay computer /1e-30/
4

0 回答 0