Android 11使用TextToSpeech实现文字转换语音&&相关bug:speak failed: not bound to TTS engine

代码

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private EditText main_edit_text;
    private TextToSpeech mTextToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_edit_text = (EditText)findViewById(R.id.message);
        mTextToSpeech = new TextToSpeech(this,new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) { //初始化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");
    }
    public void speak(View view) {
        String content = 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
    protected void onStart() {
        super.onStart();
        Log.e("activityState", "Activity_onStart");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("activityState", "Activity_onRestart");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.e("activityState", "Activity_onPause");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.e("activityState", "Activity_onResume");
    }
    @Override
    protected void onStop() {
        super.onStop();
        // 不管是否正在朗读TTS都被打断
        mTextToSpeech.stop();
        // 关闭,释放资源
        mTextToSpeech.shutdown();
    }
    @Override
    protected void onDestroy() {
        if (mTextToSpeech != null) {
            mTextToSpeech.stop();
            mTextToSpeech.shutdown();
            mTextToSpeech = null;
        }
        super.onDestroy();
    }
}

activity_main.xml

注意设置button点击动作为speak

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <EditText
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:hint="Enter a message"></EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="speak"
        android:text="Read"></Button>
</LinearLayout>

BUG记录

如果出现speak failed: not bound to TTS engine并且是Android 11

注意:模拟器是Android11版本要在AndroidManifest.xml中加入

<queries>
    <intent>
       <action android:name="android.intent.action.TTS_SERVICE" />
    </intent>
</queries>

点击查看[官方文档](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: Android TextToSpeech Example (github.com)

JocysCom/TextToSpeech: Jocys.com Text To Speech Monitor and WoW Addon - Reads quests and chat messages with text-to-speech voices. (github.com)

***YunyangBlogDemo/texttospeechdemo at master · WarmYunyang/YunyangBlogDemo (github.com)

sunfusong/NativeTTS: Android原生TTS+讯飞语音引擎实现免费纯离线的中英TTS (github.com)

文字转化为语音Android中TextToSpeech类的简单使用 - 简书 (jianshu.com)

TextToSpeech的使用_Brioal Is Hardworking-CSDN博客

android TTS TextToSpeech - 简书 (jianshu.com)

(2条消息) 安卓文字转语音——其实可以很简单——TextToSpeech用法解析_feisher-CSDN博客_安卓文字转语音

Android 11使用TextToSpeech实现文字转换语音&&相关bug:speak failed: not bound to TTS engine

上一篇:Android监听耳机按键事件


下一篇:APPCMD 精心整理 有点赞的吗