我有一个应用程序,可以播放一些文本到语音。 它运行良好,但有人抱怨说他在 "三星Galaxy Note 10+ 5G上的安卓10 "上什么都听不到。 从我所做的测试来看,似乎代码的执行没有任何错误,甚至在应该听到文字的时候有一个停顿,但没有声音传出来。 所检查的东西。

  • The TTS engine is google - but it doesn't work on both Google and Samsung.
  • In the TTS settings when you hit play, you can hear the playback sample.
  • The language is set to English US
  • The TTS init code:

    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),
                            @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);