Android Studio 文字转语音失败:未绑定到 TTS 引擎。
创始人
2024-08-16 21:30:17
0

问题描述: 在使用Android Studio开发中,尝试将文字转为语音时,出现了错误提示“未绑定到 TTS 引擎”。

解决方法: 该错误提示通常是由于未正确配置和初始化TTS引擎所导致的。以下是解决该问题的步骤和代码示例:

  1. 确保已在AndroidManifest.xml文件中添加了以下权限:




  1. build.gradle文件中添加以下依赖项:
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.speech.tts:android-tts:1.0.0'
  1. MainActivity.java文件中添加以下代码:
import android.speech.tts.TextToSpeech;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TextToSpeech(this, this);
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                // 将文字转为语音
                tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
            }
        } else {
            Log.e("TTS", "Initialization Failed!");
        }
    }

    @Override
    protected void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}

通过以上步骤和代码示例,应该能够解决“未绑定到 TTS 引擎”错误,并成功将文字转为语音。

相关内容

热门资讯

透视曝光!wepoker游戏安... 透视曝光!wepoker游戏安装教程(透视)拱趴大菠萝辅助神器,教程学习(有挂分享)-哔哩哔哩1、用...
wepoker透视脚本是什么!... wepoker透视脚本是什么!wepoker透视是真的吗(透视)教程-其实透视是有挂该软件可以轻松地...
透视教你!pokemomo辅助... 透视教你!pokemomo辅助软件(透视)hhpkoer辅助器,教程项目(竟然有挂)-哔哩哔哩pok...
wpk德州局怎么透视!wepo... wpk德州局怎么透视!wepoker国外版透视(透视)教程-切实必备真的有挂wpk德州局怎么透视辅助...
透视有挂!hhpoker视频巡... 透视有挂!hhpoker视频巡查真的假的(透视)hhpoker免费辅助器,教程攻略(有挂教学)-哔哩...
hhpoker开挂教程!wep... hhpoker开挂教程!wepoker透视苹果系统(透视)技巧-都是解谜是有挂1)wepoker透视...
透视解迷!hhpoker德州真... 透视解迷!hhpoker德州真的假的(透视)wepoker祈福有用吗,教程讲义(有挂透明挂)-哔哩哔...
德州局透视!菠萝辅助器免费版的... 德州局透视!菠萝辅助器免费版的特点(透视)app-好像了解真的有挂1、下载好菠萝辅助器免费版的特点透...
pokerrrr2辅助!wep... pokerrrr2辅助!wepoker有没有辅助(透视)插件-切实解谜真的有挂wepoker有没有辅...
透视曝光!pokemmo脚本辅... 透视曝光!pokemmo脚本辅助(透视)wepoker怎么设置透视,教程演示(发现有挂)-哔哩哔哩w...