首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android文本到语音播放没有错误,但不产生任何声音。

Android文本到语音播放没有错误,但不产生任何声音。
EN

Stack Overflow用户
提问于 2020-12-13 05:55:27
回答 1查看 697关注 0票数 0

我有一个应用程序可以播放一些文字到语音。它工作得很好,但我有个人抱怨说,他在“三星Galaxy 5G上的安卓10”上听不到任何声音。从我所做的测试来看,代码似乎是在没有任何错误的情况下执行的,甚至在应该听到文本时也会暂停,但是没有发出任何声音。检查过的东西:

  1. TTS引擎是google,但是它不能同时在Google和三星上工作。在TTS设置下的
  2. 当你点击播放时,你可以听到回放示例。
  3. 该语言被设置为英语US

TTS init代码:

代码语言:javascript
运行
复制
private void initTTS() {
    Log.d(TAG, "initTTS: Enter");
    //assume the worst
    mTTSInit = false;

    //create a TTS and do not use it until you get a confirmation that the init process went well
    mTTS = new TextToSpeech(this, status -> {

        //OnInit of TTS is run on the main thread and so is VERY slow
        new Thread(() -> {
            if (status == TextToSpeech.SUCCESS) {
                //use English - not sure about other languages at the moment.
                mTTS.setSpeechRate(0.7f);
                mTTS.setPitch(1.1f);
                int result = mTTS.setLanguage(Locale.US);

                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "Language not supported");

                    //notify the user that TTS will not work on this device
                    showToastOnUIThread(getResources().getString(R.string.TTS_missing_lang_error));
                } else {
                    Log.d(TAG, "onInit: SUCCESS");
                    //Init went fine.
                    //Set a listener when the TTS message finish as we sometime want
                    //to chime if a tile with a gem was produced.
                    mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                        @Override
                        public void onStart(String utteranceId) {
                            showToastOnUIThread("About to play message - raising volume");
                            AudioManager am = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
                            am.setStreamVolume(AudioManager.STREAM_MUSIC,
                                    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                                    0);
                        }

                        @Override
                        public void onDone(String utteranceId) {
                            Log.d(TAG, "onDone: TTS: " + utteranceId);
                            showToastOnUIThread("Message was: " + utteranceId);
                            playChimeSound();
                        }

                        @Override
                        public void onError(String utteranceId) {
                            Log.d(TAG, "onError: TTS error while trying to say: " + utteranceId);
                        }
                    });
                    mTTSInit = true;
                    runOnUiThread(() -> mTTSStatusButton.setVisibility(View.GONE));
                }
            } else {
                Log.e("TTS", "Initialization failed");
                //notify the user that TTS will not work on this device
                showToastOnUIThread(getResources().getString(R.string.TTS_missing_lang_error));
            }
        }).start();
    });
}

TTS回放代码:

代码语言:javascript
运行
复制
if (readOutLoud && mTTSInit) {
        String text = getString(R.string.tile) + " " + mViewModel.getLastSelectedTile();

        //to get a call back from TTS we mst supply a KEY_PARAM_UTTERANCE_ID
        HashMap<String, String> ttsHashMap = new HashMap<>();
        ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
        ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
        ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "1");
        mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, ttsHashMap);
    }
EN

Stack Overflow用户

回答已采纳

发布于 2020-12-17 16:20:55

试着移除:

代码语言:javascript
运行
复制
ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION)); 
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65272775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档