英文:
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() > 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();
}
}
}
<!-- end snippet -->
activity_main.xml
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?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>
<!-- end snippet -->
↓strings.xml
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<resources>
<string name="app_name">Test</string>
<string name="hint">"Please Input Something"</string>
<string name="button">Text To Speech</string>
</resources>
<!-- 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.
答案2
得分: 1
Sure, here is the translation:
如果您正在使用 SDK 版本 30,则需要返回到版本 28,它将起作用。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论