use*_*791 6 java android text-to-speech
我有一个工作文本到演讲但我想知道,而不是一个女性的声音,当应用程序调用它来播放它会做一个男性的声音而不是?
现在可以使用男/女声音并动态地从App UI更改它.像这样定义TTS(在构造函数中添加google tts引擎):
tts = new TextToSpeech(context, this, "com.google.android.tts");
Run Code Online (Sandbox Code Playgroud)
contex = activity/app
this = TextToSpeech.OnInitListener
从tts.getVoices()列表中,选择您想要的语音,其名称如下:
for (Voice tmpVoice : tts.getVoices()) {
if (tmpVoice.getName().equals(_voiceName)) {
return tmpVoice;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:你需要_voiceName通过从中获取硬编码的voice_name 来设置tts.getVoices().例如:对于英国男性,它将是:"en-us-x-sfg#male_1-local"
可以把声音变男声
这是我的代码,希望对您有所帮助!
//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);
Run Code Online (Sandbox Code Playgroud)
在 Activity 上实现 TextToSpeech.OnInitListener。
@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!");
}
}
Run Code Online (Sandbox Code Playgroud)
并添加此功能:
private void speakOut(String message) {
t1.speak(message, TextToSpeech.QUEUE_FLUSH, null);
}
Run Code Online (Sandbox Code Playgroud)
这取决于底层 TTS 引擎。有些是可配置的并且有不同的声音(男声、女声等),有些只有一种声音。在任何情况下,您都无法从您的应用程序控制此操作,用户必须从“设置”应用程序更改 TTS 引擎设置。您只能指示他们安装特定引擎,并设置您的应用程序以使用它。
| 归档时间: |
|
| 查看次数: |
17570 次 |
| 最近记录: |