Android,说话失败:TTS引擎连接未完全设置

时间:2013-12-24 16:49:25

标签: java android text-to-speech

我正在努力让TextToSpeech工作,但我得到了“说不通:TTS引擎连接没有完全设置”,但是得到了绿色:连接到ComponentInfo {... GoogleTTSService}。

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

以下是我的概述代码。(您可以在此处找到完整的代码: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的初学者......代码看起来很糟糕。

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

谢谢,圣诞快乐!

2 个答案:

答案 0 :(得分:2)

由于调用AsyncTask code,似乎永远不会执行onInit方法。 我在onInit()方法而不是onCreate()中移动了connect调用,现在它可以正常工作。

希望它会对某人有所帮助。

答案 1 :(得分:1)

这个问题很旧,但是最近我也遇到了类似的问题。就我而言,传递给speak方法的第一句话从没说过,LogCat正在显示此警告。

我通过将TextToSpeech对象放置为静态对象并创建一个static setup方法来处理它。用作singleton design pattern。我在应用程序的第一个onCreate上调用此方法。到现在为止一切正常。见下文。


public class SpeakerManager {

    private static TextToSpeech speaker;

    public static void setup() {
        if(speaker == null) {
            speaker = new TextToSpeech(MyAppUtils.getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status == TextToSpeech.SUCCESS)
                        speaker.setLanguage(Locale.getDefault());
                }
            });
        }
    }

    public static void speak(String toBeSpoken) {
        speaker.speak(toBeSpoken, TextToSpeech.QUEUE_FLUSH, null, "0000000");
    }

    public static void pause() {
        speaker.stop();
        speaker.shutdown();
    }
}

MyAppUtils来自this question

任何改进都将受到欢迎。

我希望它能对某人有所帮助。