W/TextToSpeech: 语音播放失败:未绑定到TTS引擎,声音无法工作

huangapple go评论69阅读模式
英文:

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

问题

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();
        }
    }
}
<?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>
<resources>
    <string name="app_name">Test</string>
    <string name="hint">Please Input Something</string>
    <string name="button">Text To Speech</string>
</resources>
英文:

I am now trying to implement a function that when the user clicks the button, it will speak input words inside the EditText. But I could not find how to improve my codes, any helps or advice, please.

Following this youtube video
Working with TTS in Android Studio

As I checked some sites, I tried everything, for instance,

・Change the emulator made 3 emulators

・Setting->System->Languages & input->Advanced->Text-to-speech output

・Re-install English(US)

・Preferred engine is Google Text-to-speech Engine

・Check the SDK version, and it is 30

Finally realized that 「W/TextToSpeech: speak failed: not bound to TTS engine」 appears in the android studio

↓MainActivity.java
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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() &gt; 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,&quot;Success&quot;,Toast.LENGTH_SHORT).show();
} else if (i == textToSpeech.ERROR){
Toast.makeText(this,&quot;Error&quot;,Toast.LENGTH_SHORT).show();
}
}
}

<!-- end snippet -->

activity_main.xml
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
android:gravity=&quot;center&quot;
tools:context=&quot;.MainActivity&quot; &gt;
&lt;EditText
android:id=&quot;@+id/editText&quot;
android:hint=&quot;@string/hint&quot;
android:layout_margin=&quot;20dp&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;Button
android:id=&quot;@+id/button_tts&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;20dp&quot;
android:text=&quot;@string/button&quot; /&gt;
&lt;/LinearLayout&gt;

<!-- end snippet -->

↓strings.xml
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;resources&gt;
&lt;string name=&quot;app_name&quot;&gt;Test&lt;/string&gt;
&lt;string name=&quot;hint&quot;&gt;&quot;Please Input Something&quot;&lt;/string&gt;
&lt;string name=&quot;button&quot;&gt;Text To Speech&lt;/string&gt;
&lt;/resources&gt;

<!-- end snippet -->

答案1

得分: 1

问题只是一个SDK版本,我使用了30,但是29和28的SDK版本对于实施是最好的。

在这里查看

英文:

The issue was just an SDKVersion, I used 30. but 29 and 28 SDKVersion is the best for implementation.

See here

答案2

得分: 1

Sure, here is the translation:

如果您正在使用 SDK 版本 30,则需要返回到版本 28,它将起作用。

英文:

W/TextToSpeech: 语音播放失败:未绑定到TTS引擎,声音无法工作

If your are using sdk version 30 you need to go back on 28 it will work

huangapple
  • 本文由 发表于 2020年10月9日 19:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64278940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定