Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将TextToSpeech与RecognizerIntent(android)结合使用_Java_Android_Xml_Eclipse_Voice - Fatal编程技术网

Java 将TextToSpeech与RecognizerIntent(android)结合使用

Java 将TextToSpeech与RecognizerIntent(android)结合使用,java,android,xml,eclipse,voice,Java,Android,Xml,Eclipse,Voice,我正在尝试结合使用RecognizerIntent和TextToSpeech,让手机响应用户使用RecognizerIntent发出的特定命令 onActivityResult方法中的for循环根据文本字符串数组检查来自用户的可能输入。 日志cat消息会运行,但不会显示文本 以下是我的Java代码: import java.util.ArrayList; import java.util.Locale; import android.app.Activity; import android.co

我正在尝试结合使用
RecognizerIntent
TextToSpeech
,让手机响应用户使用
RecognizerIntent
发出的特定命令
onActivityResult
方法中的for循环根据文本字符串数组检查来自用户的可能输入。
日志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();
    }
}
import java.util.ArrayList;
导入java.util.Locale;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.speech.RecognizerIntent;
导入android.speech.tts.TextToSpeech;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
公共类TextVoice扩展活动实现OnClickListener{
静态最终字符串[]文本={“你好”,“欢迎”,“很高兴认识你”};
静态最终整数检查=111;
ArrayList结果;
texttospeechtts;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.textvoice);
按钮b=(按钮)findViewById(R.id.bTextToVoice);
b、 setOnClickListener(此);
Log.println(Log.DEBUG,“voice”,“Startet”);
tts=new TextToSpeech(TextVoice.this,new TextToSpeech.OnInitListener(){
@凌驾
公共无效onInit(int状态){
if(状态!=TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
}
//@覆盖
//公共void onClick(视图v){
////TODO自动生成的方法存根
//随机r=新随机();
//字符串随机=文本[r.nextInt(3)];
//tts.speak(随机,TextToSpeech.QUEUE\u FLUSH,null);
// }
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
意图i=新意图(识别者意图、行动、识别、讲话);
i、 putExtra(RecognizerIntent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
i、 putExtra(RecognizerIntent.EXTRA_提示符,“说出来,儿子!”);
startActivityForResult(一,检查);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==check&&resultCode==RESULT\u确定){
结果=data.getStringArrayListExtra(RecognizerIntent.EXTRA_结果);
说();
}
}
受保护的void speak(){
试一试{
睡眠(10000);
}捕捉(中断异常e){
e、 printStackTrace();
}
对于(int i=0;i
如果你仔细看,你的
onPause()
会关闭你的tts,因此当你回到onActivityResult时,你的tts不再有效-你应该在使用前重新初始化它。至少这看起来是可能的原因,您应该有一些logcat错误条目,甚至异常。还调用
Thread.sleep(10000)总是一个坏主意