Android 文字转语音播放没有错误但不产生任何声音
Android Text to speech plays without error but does not produce any sound
我有一个应用程序可以播放一些文本到语音。
它运行良好,但有人抱怨说他在“Android 10 on a Samsung Galaxy Note 10+ 5G”上听不到任何声音。
从我所做的测试来看,似乎代码的执行没有任何错误,甚至在应该听到文本时出现停顿,但没有发出声音。
检查的内容:
- TTS 引擎是 google - 但它不适用于 Google 和三星。
- 在 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);
}
尝试删除:
ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
我有一个应用程序可以播放一些文本到语音。 它运行良好,但有人抱怨说他在“Android 10 on a Samsung Galaxy Note 10+ 5G”上听不到任何声音。 从我所做的测试来看,似乎代码的执行没有任何错误,甚至在应该听到文本时出现停顿,但没有发出声音。 检查的内容:
- TTS 引擎是 google - 但它不适用于 Google 和三星。
- 在 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);
}
尝试删除:
ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));