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透视方法,微乐自建房脚本使用安全吗,了解脚本(有挂总结)-哔哩哔哩wepok...
脚本透视!hhpkoer辅助挂... 脚本透视!hhpkoer辅助挂是真的吗,hhpoker有后台操作吗(透视)确实真的是有挂(哔哩哔哩)...
据玩家消息!wepoker软件... 据玩家消息!wepoker软件安装包,微乐自建房辅助神器怎么安装,教你脚本(确实有挂)-哔哩哔哩1、...
透视攻略!德州局HHpoker... 透视攻略!德州局HHpoker透视脚本,微乐小程序辅助开发透视,有挂脚本(有挂技巧)-哔哩哔哩1、德...
透视开挂!hhpoker辅助,... 透视开挂!hhpoker辅助,hh poker辅助器先试用(透视)其实是真的软件(哔哩哔哩)暗藏猫腻...
透视分享!aapoker辅助器... 透视分享!aapoker辅助器是真的吗,aapoker怎么开辅助器(透视)好像存在有挂(哔哩哔哩)1...
值得注意的是!线上德州的辅助器... 值得注意的是!线上德州的辅助器是什么,广西微乐小程序微信辅助器免费,详情透视(有挂方针)-哔哩哔哩1...
透视了解!aapoker如何设... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视攻略!hhpoker软件安... 透视攻略!hhpoker软件安装包,微乐小游戏万能修改器,详情透视(有挂秘诀)-哔哩哔哩亲,关键说明...