簡體   English   中英

結合使用TextToSpeech和RecognizerIntent(android)

[英]Using TextToSpeech together with RecognizerIntent (android)

我正在嘗試結合使用RecognizerIntentTextToSpeech來使手機響應用戶使用RecognizerIntent發出的某些命令。
onActivityResult方法中的for循環會根據文本字符串數組檢查用戶可能輸入的內容。
運行log-cat消息,但不會說出文字。

這是我的Java代碼:

import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TextVoice extends Activity implements OnClickListener {

    static final String[] texts = { "Hello", "Welcome", "Nice to meet you" };
    static final int check = 111;

    ArrayList<String> results;
    TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textvoice);

        Button b = (Button) findViewById(R.id.bTextToVoice);
        b.setOnClickListener(this);
        Log.println(Log.DEBUG, "voice", "Startet");

        tts = new TextToSpeech(TextVoice.this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.US);
                }
            }
        });
    }

    // @Override
    // public void onClick(View v) {
    //     // TODO Auto-generated method stub
    //     Random r = new Random();
    //     String random = texts[r.nextInt(3)];
    //     tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
    // }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak up, son!");
        startActivityForResult(i, check);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == check && resultCode == RESULT_OK) {
            results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            speak();
        }
    }

    protected void speak() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < results.size(); i++) {
            Log.println(Log.DEBUG, "voice", "Option " + i + ": " + results.get(i));

            if (results.get(i).equalsIgnoreCase(texts[0])) {
                Log.println(Log.DEBUG, "voice", "Registrerte: hello");
                tts.speak(texts[0], TextToSpeech.QUEUE_ADD, null);
            } else if (results.get(i).equalsIgnoreCase(texts[1])) {
                tts.speak(texts[1], TextToSpeech.QUEUE_ADD, null);
            } else if (results.get(i).equalsIgnoreCase(texts[2])) {
                tts.speak(texts[2], TextToSpeech.QUEUE_ADD, null);
            } else {
                tts.speak("Didn't here you there", TextToSpeech.QUEUE_ADD, null);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}

如果您仔細看,您的onPause()將關閉您的tts,因此當您返回onActivityResult時,您的tts不再有效-您應在使用前再次將其重新初始化。 至少這看起來像是可能的原因,您應該有一些logcat錯誤條目,甚至是異常。 還調用Thread.sleep(10000); 在UI線程上總是一個壞主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM