第二次收听SpeechRecognizer时出错

时间:2016-08-10 09:04:42

标签: java android speech-recognition

我在android上遇到SpeechRecognizer的问题。这是我的代码:

public class MyRecognizerListener implements RecognitionListener {

    String id;

    MyRecognizerListener(String id){
        this.id = id;
    }

    @Override
    public void onEndOfSpeech() {
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.d("Speech", "Inizia ad Ascoltare");
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d("Speech", "E' pronto ad Ascoltare");
    }

    @Override
    public void onPartialResults(Bundle results) {
            matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

            if(matches.size() > 1) {

                Log.d("Speech", "Risultati Parziali = " + Integer.toString(matches.size()));

                for (int i = 0; i < matches.size(); i++) {
                    Log.d("Speech", matches.get(i));
                }

                if (matches.contains("si") || matches.contains("sì") || matches.contains("Sì")
                        || matches.contains("yes") || matches.contains("ES")) {
                    Opera opera = createOpera(id);
                    startAudio(opera);
                }

                matches = null;
                turnSpeechOff = false;
                speechRecognizer.cancel();
                speechRecognizer.destroy();
                speechRecognizer = null;
            }
    }

    @Override
    public void onEvent(int eventType, Bundle params) {

    }

    @Override
    public void onError(int error) {

        if(turnSpeechOff) {
            Log.d("Speech", Integer.toString(error));
            turnSpeechOff = false;
            speechRecognizer.cancel();
            speechRecognizer.destroy();
            speechRecognizer = null;

        }

    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onResults(Bundle results) {
        matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        for (int i = 0; i < matches.size(); i++){
            Log.d("Parole",  matches.get(i));
        }

        if(matches.contains("si") || matches.contains("sì") || matches.contains("Sì")
                || matches.contains("yes") || matches.contains("ES")){
            Opera opera = createOpera(id);
            startAudio(opera);
        }

        matches = null;
        turnSpeechOff = false;
        speechRecognizer.cancel();
        speechRecognizer.destroy();
        speechRecognizer = null;
    }

}

public void startAudioAsk(final String art_id){

    if(speechRecognizer != null) {
        return;
    }
    if(audioPlayer == null || !audioPlayer.isPlaying() ) {

        if(operaAudioAsk != null && operaAudioAsk.equals(art_id)){
            return;
        }

        operaAudioAsk = art_id;
        if(language.equals("IT")) {
            audioPlayer = MediaPlayer.create(this, R.raw.chiedi);
        } else {
            audioPlayer = MediaPlayer.create(this, R.raw.ask);
        }
        audioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioPlayer.reset();
                audioPlayer.release();
                int audioTitle;
                if(language.equals("IT")){
                    audioTitle = getResources().getIdentifier(art_id + "_title" + "_it", "raw", getPackageName());
                    audioPlayer = MediaPlayer.create(MainActivity.this, audioTitle);
                } else {
                    audioTitle = getResources().getIdentifier(art_id + "_title" + "_en", "raw", getPackageName());
                    audioPlayer = MediaPlayer.create(MainActivity.this, audioTitle);
                }
                audioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        askVoice(art_id);
                        audioPlayer.reset();
                        audioPlayer.release();
                        audioPlayer = null;
                    }
                });
                audioPlayer.start();
            }
        });

        audioPlayer.start();
    }
}

public void askVoice(String art_id){
    if(speechRecognizer == null) {
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        speechRecognizer.setRecognitionListener(new MyRecognizerListener(art_id));
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
        intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
        if(language.equals("IT")){
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "it");
        } else {
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en_US");
        }
        speechRecognizer.startListening(intent);
        voiceTimer = new Timer();
        voiceTimer.schedule(new StopVoiceManager(), 2000);
        Log.d("Speech", "Starta");
    }
}

public void stopVoice(){
    if(speechRecognizer != null){
        Log.d("Speech", "Cancello");
        turnSpeechOff = true;
        speechRecognizer.stopListening();
    }
}

public class StopVoiceManager extends TimerTask{

    @Override
    public void run() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopVoice();
                Log.d("Speech", "Prova a Cancellare");
            }
        });
    }
}

正如您所看到的,还有一项任务在2秒后调用speechRecognizer.stoplistening()。

第一次听是没问题,我说&#34;是&#34;并且它识别它,但是第二次听取提出了ERROR_CLIENT并且它没有识别任何东西,那么第三次听力恢复正常,第四次听不到任何东西。

我该如何修复这个错误?

0 个答案:

没有答案