0

I'm trying to read aloud hindi text via google TTS voice synthesizer, it is working fine in my device, since I already installed hindi voice data manually, but when upon testing on another device, which does not contain hindi voice, it is not showing prompt for downloading particular voice data. Here is the partial code:

  @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == ACT_CHECK_TTS_DATA) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // Data exists, so we instantiate the TTS engine
            textToSpeech = new TextToSpeech(this, this);
        } else {
            // Data is missing, so we start the TTS
            // installation process
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}

So, TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA seems not working, However I do not know exactly what it does, rest of the code is fine, I believe. Any solution to my problem?

Rahul Singh
  • 243
  • 3
  • 15
  • what does the log tell after you started the activity to install TTS data? By the way, the constant at the first if-case should be: `ACTION_CHECK_TTS_DATA` – Raptor Dec 10 '18 at 06:21
  • I'm unable to use network for reading like Google Translate does. I'm not getting any error by the way. so log is not saying anything. – Rahul Singh Dec 10 '18 at 06:24
  • In if clause, ACT_CHECK_TTS_DATA is just user defined const for tracking onActivityResult ID. which value is 1000. – Rahul Singh Dec 10 '18 at 06:26
  • @VVB bro, I already put this in onCreate: Intent ttsIntent = new Intent(); ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(ttsIntent, ACT_CHECK_TTS_DATA); – Rahul Singh Dec 10 '18 at 06:31
  • Are you trying on emulator or real device? – VVB Dec 10 '18 at 06:32
  • Real device, moto – Rahul Singh Dec 10 '18 at 06:33
  • 1
    I just wanted to know, How can I prompt for download if required voice data is missing from target device? In real time. – Rahul Singh Dec 10 '18 at 06:33
  • 1
    You should add debug message at each if-branch to see whether the program flow is correct or not. Also, did you specify the language you want to download (i.e. Hindi)? You probably don't want to download all languages, right? – Raptor Dec 10 '18 at 06:57

1 Answers1

2

Use below way to check avialble language installed or not.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);

   if (requestCode == VOICE_DATA_CHECK) {
    ArrayList<String> availableLanguages = 
   data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);

     if (availableLanguages.isEmpty()) {
      // no language data available, prompt for install
     Intent installIntent = new Intent();
      installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
      startActivity(installIntent);
     } else {
     // some language data is available, create TTS instance
      mTts = new TextToSpeech(this, this);
   }
  }
}
vishal jangid
  • 2,967
  • 16
  • 22