Android Text To Speech Male Voice
I have a working text to speech but I was wondering instead of a female voice when the app calls it to be played it will do a male voice instead?
Solution 1:
It is now possible to use male/female voice and change it from App UI dynamically. Define TTS like this (add google tts engine in constructor):
tts = new TextToSpeech(context, this, "com.google.android.tts");
contex = activity/app
this= TextToSpeech.OnInitListener
From tts.getVoices()
list, chose your desired voice by it's name like this:
for (Voice tmpVoice : tts.getVoices()) {
if (tmpVoice.getName().equals(_voiceName)) {
return tmpVoice;
break;
}
}
N.B: U need to set _voiceName
by getting hard coded voice_name from tts.getVoices()
. e.g: for English male it would be: "en-us-x-sfg#male_1-local"
Solution 2:
It is possible to change voice into male
here is my code,hope it will help you!
//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);
implements 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!");
}
}
And add this function also:
private void speakOut(String message) {
t1.speak(message, TextToSpeech.QUEUE_FLUSH, null);
}