如何解决W / TextToSpeech:语音失败:未绑定到TTS引擎,声音不起作用
我现在正在尝试实现一个功能,当用户单击按钮时,它将在EditText内说出输入单词。但是我找不到如何改进代码,任何帮助或建议。
关注此youtube视频 Working with TTS in Android Studio
例如,当我检查一些网站时,我尝试了所有事情,
·将模拟器更改为3个模拟器
・设置->系统->语言和输入->高级->文本到语音输出
・重新安装英文(美国)
・首选引擎为Google文字转语音引擎
・确认SDK版本为30
最终意识到在Android Studio中出现了“ W / TextToSpeech:语音失败:未绑定到TTS引擎”
↓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>
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。