android11 tts,Android 11 使用 TextToSpeech 实现文字转换语音 && 相关 bug:speak failed: not bound to TTS engine...
Android 11 使用 TextToSpeech 实现文字转换语音 && 相关 bug:speak failed: not bound to TTS engine代码MainActivity.javapackagecom.example.myapplication;importandroidx.appcompat.App.AppCompatActivity;importAndr
Android 11 使用 TextToSpeech 实现文字转换语音 && 相关 bug:speak failed: not bound to TTS engine
代码
MainActivity.javapackagecom.example.myapplication;
importandroidx.appcompat.App.AppCompatActivity;
importAndroid.content.Intent;
importAndroid.os.Bundle;
importAndroid.speech.tts.TextToSpeech;
importAndroid.text.TextUtils;
importAndroid.util.Log;
importAndroid.view.View;
importAndroid.widget.Button;
importAndroid.widget.EditText;
importAndroid.widget.Toast;
importjava.util.Locale;
publicclassMainActivityextendsAppCompatActivity{
privateEditTextmain_edit_text;
privateTextToSpeechmTextToSpeech;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_edit_text=(EditText)findViewById(R.id.message);
mTextToSpeech=newTextToSpeech(this,newTextToSpeech.OnInitListener(){
@Override
publicvoidonInit(intstatus){// 初始化 tts
if(status==TextToSpeech.SUCCESS){
// 初始化成功
Log.e("error","haha");
}
else{
Log.e("error","呕");
Toast.makeText(MainActivity.this,"初始化失败",Toast.LENGTH_SHORT);
}
}
});
Log.e("activityState","Activity_onCreate");
}
publicvoidspeak(Viewview){
Stringcontent=main_edit_text.getText().toString();
Log.e("error",content);
if(content.isEmpty()){
mTextToSpeech.speak("空空",TextToSpeech.QUEUE_ADD,null);
}else{
mTextToSpeech.speak(content,TextToSpeech.QUEUE_ADD,null);
}
}
@Override
protectedvoidonStart(){
super.onStart();
Log.e("activityState","Activity_onStart");
}
@Override
protectedvoidonRestart(){
super.onRestart();
Log.e("activityState","Activity_onRestart");
}
@Override
protectedvoidonPause(){
super.onPause();
Log.e("activityState","Activity_onPause");
}
@Override
protectedvoidonResume(){
super.onResume();
Log.e("activityState","Activity_onResume");
}
@Override
protectedvoidonStop(){
super.onStop();
// 不管是否正在朗读 TTS 都被打断
mTextToSpeech.stop();
// 关闭, 释放资源
mTextToSpeech.shutdown();
}
@Override
protectedvoidonDestroy(){
if(mTextToSpeech!=null){
mTextToSpeech.stop();
mTextToSpeech.shutdown();
mTextToSpeech=null;
}
super.onDestroy();
}
}
activity_main.xml
注意设置 button 点击动作为 speak<?xml version="1.0"encoding="utf-8"?>
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="horizontal"
>
Android:id="@+id/message"
Android:layout_width="wrap_content"
Android:layout_height="match_parent"
Android:layout_weight="1"
Android:hint="Enter a message">
Android:id="@+id/button"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:onClick="speak"
Android:text="Read">
BUG 记录
如果出现 speak failed: not bound to TTS engine 并且是 Android 11
注意: 模拟器是 Android11 版本要在 AndroidManifest.xml 中加入
点击查看 [官方文档](TextToSpeech | Android 开发者 | Android Developers (google.cn))
相关链接Android-TextToSpeech-Example/TTSManager.java at master.stacktipslab/Android-TextToSpeech-Example(GitHub.com)
Android-TextToSpeech/MainActivity.java at master.tutsplus/Android-TextToSpeech(GitHub.com)
Android-TextToSpeech/Speaker.java at master.tutsplus/Android-TextToSpeech(GitHub.com)
stacktipslab/Android-TextToSpeech-Example:AndroidTextToSpeechExample(GitHub.com)
JocysCom/TextToSpeech:Jocys.comTextToSpeechMonitorandWoWAddon-Readsquestsandchat messageswithtext-to-speech voices.(GitHub.com)https://github.com/JocysCom/TextToSpeech
***YunyangBlogDemo/texttospeechdemo at master.WarmYunyang/YunyangBlogDemo(GitHub.com)
sunfusong/NativeTTS: Android 原生 TTS + 讯飞语音引擎实现免费纯离线的中英 TTS (GitHub.com) https://github.com/sunfusong/NativeTTS
文字转化为语音 Android 中 TextToSpeech 类的简单使用 - 简书 (jianshu.com) https://www.jianshu.com/p/da6af26b7483
TextToSpeech 的使用_Brioal Is Hardworking-CSDN 博客
Android TTS TextToSpeech - 简书 (jianshu.com) https://www.jianshu.com/p/385399207d8f
(2 条消息) 安卓文字转语音 -- 其实可以很简单 --TextToSpeech 用法解析_feisher-CSDN 博客_安卓文字转语音
来源: http://www.bubuko.com/infodetail-3709377.html
更多推荐


所有评论(0)