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 引擎”错误,并成功将文字转为语音。

相关内容

热门资讯

3分钟教程书!aapoker透... 3分钟教程书!aapoker透视方法(透视)果然有辅助透视(哔哩哔哩)1、aapoker透视方法有没...
第9分钟步骤!WePoKer辅... 第9分钟步骤!WePoKer辅助器(透视)一直是有辅助插件(哔哩哔哩)1、第9分钟步骤!WePoKe...
七分钟机巧!wejoker手机... 七分钟机巧!wejoker手机版透视脚本(透视)竟然真的有辅助透视(哔哩哔哩)1、七分钟机巧!wej...
第九分钟机巧!德州真人透视脚本... 第九分钟机巧!德州真人透视脚本(透视)确实存在有辅助工具(哔哩哔哩)在进入德州真人透视脚本软件靠谱后...
第1分钟方案!wepoker免... 第1分钟方案!wepoker免费脚本弱密码(透视)一贯存在有辅助神器(哔哩哔哩)小薇(辅助器软件下载...
第八分钟技法!智星德州插件最新... 第八分钟技法!智星德州插件最新版本更新内容详解(透视)真是是真的有辅助脚本(哔哩哔哩)1、这是跨平台...
第九分钟学习!epoker免费... 第九分钟学习!epoker免费透视脚本(透视)竟然真的有辅助脚本(哔哩哔哩)1、金币登录送、破产送、...
六分钟手筋!pokemmo免费... 六分钟手筋!pokemmo免费脚本(透视)确实是有辅助下载(哔哩哔哩)1、进入到pokemmo免费脚...
第二分钟举措!pokerrrr... 第二分钟举措!pokerrrr2辅助(透视)本来真的是有辅助脚本(哔哩哔哩)1、点击下载安装,pok...
第八分钟积累!wejoker辅... 第八分钟积累!wejoker辅助软件价格(透视)一贯真的有辅助安装(哔哩哔哩)wejoker辅助软件...