W/TextToSpeech: speak failed: 未绑定到 TTS 引擎,声音不起作用

W/TextToSpeech: speak failed: not bound to TTS engine, Sound does not work

我现在正在尝试实现一个功能,当用户单击按钮时,它将在 EditText 中读出输入的单词。但是我找不到如何改进我的代码,请提供任何帮助或建议。

关注这个 youtube 视频 Working with TTS in Android Studio

当我检查一些网站时,我尝试了一切,例如,

・换模拟器做了3个模拟器

・设置->系统->语言和输入->高级->文字转语音输出

・重新安装英语(美国)

・首选引擎是 Google 文本转语音引擎

・查看SDK版本,是30

终于发现android工作室出现了「W/TextToSpeech: speak failed: not bound to TTS engine」

↓MainActivity.java

package com.example.sounttest2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{

    int MY_DATA_CHECK_CODE = 1000;
    TextToSpeech textToSpeech;
    EditText editText;
    Button button;

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

        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button_tts);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = editText.getText().toString();
                if(text.length() > 0){
                    textToSpeech.speak(text,TextToSpeech.QUEUE_ADD, null);
                }
            }
        });

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
                textToSpeech = new TextToSpeech(this, this);
            } else {
                Intent intent = new Intent();
                intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(intent);
        }
    }}

    @Override
    public void onInit(int i) {
        if(i == textToSpeech.SUCCESS){
            Toast.makeText(this,"Success",Toast.LENGTH_SHORT).show();
        } else if (i == textToSpeech.ERROR){
            Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText"
        android:hint="@string/hint"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_tts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/button" />
</LinearLayout>

↓strings.xml

<resources>
    <string name="app_name">Test</string>
    <string name="hint">"Please Input Something"</string>
    <string name="button">Text To Speech</string>
</resources>

问题只是一个SDK版本,我用的是30。但是29和28的SDK版本最适合实现。

See here

如果您使用的是 30 版 SDK,则需要返回 28 版才能正常工作