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博客_安卓文字轉語音


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM