安卓文字转语音 男声
2022-09-04 00:37:11
我有一个工作文本到语音,但我想知道当应用程序调用它来播放时,它会做一个男性的声音代替吗?
我有一个工作文本到语音,但我想知道当应用程序调用它来播放时,它会做一个男性的声音代替吗?
现在可以使用男性/女性语音,并从应用 UI 动态更改它。像这样定义TTS(在构造函数中添加谷歌tts引擎):
tts = new TextToSpeech(context, this, "com.google.android.tts");
contex = 活动/应用
this= TextToSpeech.OnInitListener
从列表中,按其名称选择所需的语音,如下所示:tts.getVoices()
for (Voice tmpVoice : tts.getVoices()) {
if (tmpVoice.getName().equals(_voiceName)) {
return tmpVoice;
break;
}
}
注意:您需要通过从 中获取硬编码voice_name来设置。例如:对于英国男性,它将是:“en-us-x-sfg#male_1-local”_voiceName
tts.getVoices()
可以将声音变为男性
这是我的代码,希望它能帮助你!
//onCreate
T2S= new TextToSpeech(testApp.getInstance().getApplicationContext(), this, "com.google.android.tts");
Set<String> a=new HashSet<>();
a.add("male");//here you can give male if you want to select male voice.
Voice v=new Voice("en-us-x-sfg#male_2-local",new Locale("en","US"),400,200,true,a);
T2S.setVoice(v);
T2S.setSpeechRate(0.8f);
实现 TextToSpeech.OnInitListener on Activity。
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Set<String> a=new HashSet<>();
a.add("male");//here you can give male if you want to select male voice.
//Voice v=new Voice("en-us-x-sfg#female_2-local",new Locale("en","US"),400,200,true,a);
Voice v=new Voice("en-us-x-sfg#male_2-local",new Locale("en","US"),400,200,true,a);
T2S.setVoice(v);
T2S.setSpeechRate(0.8f);
// int result = T2S.setLanguage(Locale.US);
int result = T2S.setVoice(v);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
// btnSpeak.setEnabled(true);
speakOut(mMessageVoice);
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
并添加此函数:
private void speakOut(String message) {
t1.speak(message, TextToSpeech.QUEUE_FLUSH, null);
}