【问题标题】:Android, speak failed: TTS engine connection not fully set upAndroid,说话失败:TTS 引擎连接未完全建立
【发布时间】:2019-07-25 20:44:22
【问题描述】:

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

我没有找到解决办法来解决它,而且 google 也没有给我有趣的东西。

这是我的代码概览。(你可以在这里找到完整的代码: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 的初学者......代码看起来很糟糕。

如果有人能解释我的错误,最好给我一个答案,那应该很好。

谢谢,圣诞快乐!

【问题讨论】:

    标签: java android text-to-speech


    【解决方案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

    欢迎任何改进。

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

    【讨论】:

    • 现在这是一个经典的方法。谢谢。
    【解决方案2】:

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

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2016-05-23
      • 2016-05-18
      • 2012-07-31
      • 1970-01-01
      • 2014-02-25
      • 2016-05-18
      • 1970-01-01
      相关资源
      最近更新 更多