android文本到语音播放没有错误,但不会产生任何声音

06odsfpq  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(257)

我有一个应用程序,可以播放一些文字到语音。它工作得很好,但我有人抱怨说,他听不到任何关于“三星galaxy note 10+5g上的android 10”。从我所做的测试来看,代码似乎是没有任何错误的执行,甚至有暂停时,文本应该听到,但没有声音出来。检查内容:
tts引擎是google,但它不能同时在google和samsung上工作。
在tts设置中,当您点击播放时,您可以听到播放示例。
语言设置为英语
tts初始化代码:

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播放代码:

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);
    }
w8f9ii69

w8f9ii691#

尝试删除:

ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));

相关问题