Android,说话失败:TTS 引擎连接未完全设置 [英] Android, speak failed: TTS engine connection not fully set up

查看:81
本文介绍了Android,说话失败:TTS 引擎连接未完全设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的 TextToSpeech 正常工作,但我收到了说话失败:TTS 引擎连接未完全设置"的消息,但显示为绿色:已连接到 ComponentInfo{...GoogleTTSService}.

I'm trying to get my TextToSpeech working, but I got a "speak failed: TTS engine connection not fully set up", but got in green : Connected to ComponentInfo{...GoogleTTSService}.

我没有找到修复它的解决方案,谷歌也没有给我有趣的东西.

I didn't found a solution to fix it, and google don't give me interesting things.

这是我的代码概览.(你可以在这里找到完整的代码:查看文档

Here is my code to have an overview.(you can find the complete code here : See docs

import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class MainActivity extends Activity implements OnInitListener
{

    protected static final int RESULT_SPEECH = 1;

    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    public TextToSpeech myTTS;
    protected static final int MY_DATA_CHECK_CODE = 0;


    @Override
    public void onInit(int status) {       
        if (status == TextToSpeech.SUCCESS) {
            int result = myTTS.setLanguage(Locale.getDefault());

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(MainActivity.this,
                        "This Language is not supported", Toast.LENGTH_LONG).show();                    
            }
            else {
                Toast.makeText(MainActivity.this,
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
            }
        }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(MainActivity.this,
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

        editText = (EditText) findViewById(R.id.editText);
        send = (Button)findViewById(R.id.send_button);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String message = editText.getText().toString();

                //add the text in the arrayList
                arrayList.add("> " + message);

                //sends the message to the server
                if (mTcpClient != null) {
                    mTcpClient.sendMessage(message);
                }

                //refresh the list
                mAdapter.notifyDataSetChanged();
                editText.setText("");
            }
        });

        Button btnSpeak = (Button) findViewById(R.id.speak_button);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    editText.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "Opps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });


    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                editText.setText(text.get(0));
                send.performClick();
            }
            break;
        }
        case MY_DATA_CHECK_CODE: {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
            break;
        }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myTTS.shutdown();
    }

我是 Java/Android SDK 的初学者……代码看起来很糟糕.

I'm a very very beginner with Java / Android SDK ... Code could look like very crappy.

如果有人能解释我的错误,最重要的是,给我一个答案,那应该非常好.

If someone can explain me the error, and best of all, give me an answer, it should be very nice.

谢谢,圣诞快乐!

推荐答案

由于调用了 AsyncTask 代码.我在 onInit() 方法而不是 onCreate() 方法中移动了连接调用,现在它可以工作了.

It seems the method onInit is never executed due to the call to the AsyncTask code. I moved the connect call in the onInit() method instead of the onCreate() and it now works.

希望对某人有所帮助.

这篇关于Android,说话失败:TTS 引擎连接未完全设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆