0

I want to use male voice in text speech, checked everywhere but same code is available everywhere. I used forloop to get voices, only once i get the exact voice for male and female then after i checked the same apk in my device its only giving the female voice. Below are my code.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tts = new TextToSpeech(this, this);
}

Now the init() method is given below:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onInit(int status) {
       Set<String> a = new HashSet<>();
        a.add("male");//hi-in-x-cfn#male_3-local  Voice[Name: hi-in-x-cfn#male_3-local, locale: //hi_IN, quality: 400, latency: 200, requiresNetwork: false, features: [networkTimeoutMs, //networkRetriesCount]]
       
        Voice v = new Voice("hi-in-x-cfn#male_3-local",new Locale("hi","IN"),400,200,true,a);

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setVoice(v);
            tts.setSpeechRate(0.8f);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();
            } else {
                button.setEnabled(true);
            }

        } else {
            Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();
        }
    }

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void speakOut() {

    tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String s) {
            final String keyword = s;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Voice Started", Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onDone(String s) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Done ", Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onError(String s) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Error ", Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

    Bundle params = new Bundle();
    params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");

    String text = editText.getText().toString();
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, "Dummy String");
}

Now for checking i used for loop that where i am missing , now for the same device and hi_In Locale i am getting the different value in

    for (Voice tmpVoice : tts.getVoices()) {
        
}

These are the values i found for hindi (hi_IN) in forloop tts.getVoices() method, Which value i should use and how can use these values for male voice.

1 > Voice[Name: hi-in-x-hia-local, locale: hi_IN, quality: 400, latency: 200, requiresNetwork: false, features: [networkTimeoutMs, networkRetriesCount]]

2 > Voice[Name: hi-in-x-hid-network, locale: hi_IN, quality: 400, latency: 200, requiresNetwork: true, features: [networkTimeoutMs, networkRetriesCount]]

3 > Voice[Name: hi-in-x-hie-network, locale: hi_IN, quality: 400, latency: 200, requiresNetwork: true, features: [networkTimeoutMs, networkRetriesCount]]

4 > Voice[Name: hi-in-x-cfn-local, locale: hi_IN, quality: 400, latency: 200, requiresNetwork: false, features: [networkTimeoutMs, networkRetriesCount]]

5 > Voice[Name: hi-in-x-hic-network, locale: hi_IN, quality: 400, latency: 200, requiresNetwork: true, features: [networkTimeoutMs, networkRetriesCount]]

6 > Voice[Name: hi-IN-language, locale: hi_IN, quality: 400, latency: 200, requiresNetwork: false, features: [networkTimeoutMs, legacySetLanguageVoice, networkRetriesCount]]

shanu
  • 1
  • 4
  • I presume you've seen this: https://stackoverflow.com/questions/9815245/android-text-to-speech-male-voice/67515136#67515136 ? I wrote a new answer to it which may help understanding how voices work. – Nerdy Bunz May 13 '21 at 07:08
  • Does this answer your question? [Android Text To Speech Male Voice](https://stackoverflow.com/questions/9815245/android-text-to-speech-male-voice) – Nerdy Bunz May 13 '21 at 07:08

0 Answers0