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



我正在尝试让我的TextToSpeech工作,但我得到一个"说话失败:TTS引擎连接未完全设置",但显示为绿色:连接到组件信息{...GoogleTTSService}.

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

这是我的代码来概述。(您可以在此处找到完整的代码:请参阅文档

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...代码可能看起来非常糟糕。

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

谢谢,圣诞快乐!

这个问题很老了,但我最近遇到了类似的问题。在我的例子中,传递给speak方法的第一句话从未说过,LogCat 显示此警告。

我通过将TextToSpeech对象放置为静态并创建一个static setup方法来处理它。用作单例设计模式。我在应用的第一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来自这个问题。

任何改进都将受到欢迎。

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

由于调用 AsyncTask 代码,似乎 onInit 方法从未执行。我在 onInit() 方法中移动了连接调用而不是 onCreate(),它现在可以工作了。

希望它能帮助某人。

最新更新