android 自带TextToSpeech没有声音

本文仅记录项目所遇问题

出现该问题的手机:小米手机

原因及解决方法:

原先设置是度秘语音引擎3.0,改为科大讯飞语音引擎3.0即可

具体为什么会如此,后续研究

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 Android Studio 中实现简单的对话功能,可以使用 Android 自带TextToSpeech 和 SpeechRecognizer 类。 TextToSpeech 可以将文本转换为语音,SpeechRecognizer 可以将语音转换为文本。 以下是一个简单的示例代码,可以在用户说出“你好”时使用 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); // 初始化 TextToSpeech 对象 tts = new TextToSpeech(this, this); // 初始化 SpeechRecognizer 对象 SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this); recognizer.setRecognitionListener(new RecognitionListener() { @Override public void onReadyForSpeech(Bundle params) { } @Override public void onBeginningOfSpeech() { } @Override public void onRmsChanged(float rmsdB) { } @Override public void onBufferReceived(byte[] buffer) { } @Override public void onEndOfSpeech() { } @Override public void onError(int error) { } @Override public void onResults(Bundle results) { // 获取识别结果 ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if (matches != null && matches.size() > 0) { String text = matches.get(0); if ("你好".equals(text)) { // 回复“你好” tts.speak("你好", TextToSpeech.QUEUE_FLUSH, null); } } } @Override public void onPartialResults(Bundle partialResults) { } @Override public void onEvent(int eventType, Bundle params) { } }); // 开始识别语音 recognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)); } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { // 设置语音引擎 int result = tts.setLanguage(Locale.CHINA); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Log.e("TTS", "Language not supported"); } } else { Log.e("TTS", "Init failed"); } } @Override protected void onDestroy() { // 释放 TextToSpeech 资源 if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); } } ``` 需要注意的是,使用 SpeechRecognizer 时需要用户授权使用麦克风权限。可以在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.RECORD_AUDIO" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值