Java Code Examples for android.speech.tts.TextToSpeech#setLanguage()

The following examples show how to use android.speech.tts.TextToSpeech#setLanguage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestingActivity.java    From ibm-wearables-android-sdk with Apache License 2.0 7 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing);

    detectedGesture = (TextView) findViewById(R.id.detectedGesture);
    score = (TextView) findViewById(R.id.scoring);

    detectedLayout = findViewById(R.id.detectedLayout);
    sensingLayout = findViewById(R.id.sensingLayout);

    getSupportActionBar().setTitle(R.string.testing_title);

    RecordApplication application = (RecordApplication) getApplication();
    controller = application.controller;


    textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.US);
            }
        }
    });
}
 
Example 2
Source File: QaActivity.java    From tflite-android-transformers with Apache License 2.0 6 votes vote down vote up
@Override
protected void onStart() {
  Log.v(TAG, "onStart");
  super.onStart();
  handler.post(
      () -> {
        qaClient.loadModel();
        qaClient.loadDictionary();
      });

  textToSpeech =
      new TextToSpeech(
          this,
          status -> {
            if (status == TextToSpeech.SUCCESS) {
              textToSpeech.setLanguage(Locale.US);
            } else {
              textToSpeech = null;
            }
          });
}
 
Example 3
Source File: MediaController.java    From ankihelper with GNU General Public License v3.0 6 votes vote down vote up
public void setTextToSpeech(final Context context) {
    mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                mTextToSpeech.setLanguage(Locale.UK);
                mTextToSpeech.setSpeechRate(0.70f);
            }

            mTextToSpeech.setOnUtteranceCompletedListener(
                    new TextToSpeech.OnUtteranceCompletedListener() {
                        @Override
                        public void onUtteranceCompleted(String utteranceId) {
                            ((AppCompatActivity) context).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mIsSpeaking) {
                                        callbacks.highLightTTS();
                                    }
                                }
                            });
                        }
                    });
        }
    });
}
 
Example 4
Source File: Accessibility.java    From OsmGo with MIT License 6 votes vote down vote up
@PluginMethod()
public void speak(PluginCall call) {
  final String value = call.getString("value");
  final String language = call.getString("language", "en");
  final Locale locale = Locale.forLanguageTag(language);

  if (locale == null) {
    call.error("Language was not a valid language tag.");
    return;
  }

  tts = new TextToSpeech(getContext(), new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int i) {
      tts.setLanguage(locale);
      tts.speak(value, TextToSpeech.QUEUE_FLUSH, null, "capacitoraccessibility" + System.currentTimeMillis());
    }
  });

  // Not yet implemented
  throw new UnsupportedOperationException();
}
 
Example 5
Source File: Speech.java    From 10000sentences with Apache License 2.0 6 votes vote down vote up
public Speech(Context context, Language language) {
    this.context = context;
    this.locale = findLocale(language);
    this.languageFound = locale != null;
    this.enabled = Preferences.isUseTTS(context);
    if (!this.enabled) {
        return;
    }
    tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {
            Speech.this.initialized = true;
        }
    });
    tts.setPitch(1);
    tts.setSpeechRate(0.75F);
    tts.setLanguage(locale);
}
 
Example 6
Source File: MainActivity.java    From Paideia with MIT License 6 votes vote down vote up
@Override
protected void onCreate(final Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

  setContentView(R.layout.activity_camera);

  tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
        if(status != TextToSpeech.ERROR) {
            tts.setLanguage(Locale.US);
        }
    }
  });
  if (null == savedInstanceState) {
    getFragmentManager()
        .beginTransaction()
        .replace(R.id.container, CameraConnectionFragment.newInstance())
        .commit();
  }
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.setStatusBarColor(baseColor);
}
 
Example 7
Source File: MainActivity.java    From Beginner-Level-Android-Studio-Apps with GNU General Public License v3.0 5 votes vote down vote up
public void createTTS()
{
    t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {
            if(i==TextToSpeech.SUCCESS)
            {
                t1.setLanguage(Locale.ENGLISH);
            }
        }
    });
}
 
Example 8
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {
  if (requestCode == TTS_DATA_CHECK) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
      tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        public void onInit(int status) {
          if (status == TextToSpeech.SUCCESS) {
            ttsIsInit = true;
            if (tts.isLanguageAvailable(Locale.UK) >= 0)
              tts.setLanguage(Locale.UK);
            tts.setPitch(0.8f);
            tts.setSpeechRate(1.1f);
            speak();
          }
        }
      });
    } else {
      Intent installVoice = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
      startActivity(installVoice);
    }
  }

  // Listing 14-3: Finding the results of a speech recognition request
  if (requestCode == VOICE_RECOGNITION && resultCode == RESULT_OK) {
    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    float[] confidence = data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);
    // TODO Do something with the recognized voice strings
  }
}
 
Example 9
Source File: PTextToSpeech.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public PTextToSpeech(AppRunner appRunner) throws InterruptedException {

        mTts = new TextToSpeech(appRunner.getAppContext(), status -> {
            if (status == TextToSpeech.SUCCESS) {
                mTts.setLanguage(Locale.getDefault());
            } else {
                MLog.d(TAG, "Could not initialize TextToSpeech.");
            }
        });
    }
 
Example 10
Source File: A2dpSinkActivity.java    From sample-bluetooth-audio with Apache License 2.0 5 votes vote down vote up
private void initTts() {
    mTtsEngine = new TextToSpeech(A2dpSinkActivity.this,
            new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status == TextToSpeech.SUCCESS) {
                        mTtsEngine.setLanguage(Locale.US);
                    } else {
                        Log.w(TAG, "Could not open TTS Engine (onInit status=" + status
                                + "). Ignoring text to speech");
                        mTtsEngine = null;
                    }
                }
            });
}
 
Example 11
Source File: TextToSpeechManagerImpl.java    From mirror with Apache License 2.0 5 votes vote down vote up
@Override
public void onFeatureResume() {
    mTextToSpeech = new TextToSpeech(mAppManager.getAppContext(), status -> {
        if (status == TextToSpeech.SUCCESS) {
            mTextToSpeech.setLanguage(Locale.US);
        } else {
            mTextToSpeech = null;
        }
    });
}
 
Example 12
Source File: MainActivity.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
private void initTTS() {
    ttsEngine = new TextToSpeech(this, new OnInitListener() {
        @Override
        public void onInit(int initStatus) {
            if (initStatus == TextToSpeech.SUCCESS) {
                ttsEngine.setLanguage(Locale.US);
                ttsReady = true;
            } else {
                Log.d(TAG, "Can't initialize TextToSpeech");
            }

        }
    });
}
 
Example 13
Source File: MainActivity.java    From android-apps with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etTxt=(EditText)findViewById(R.id.etTxt);
    btnPlay=(Button)findViewById(R.id.btnPlay);

    tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                tts.setLanguage(Locale.UK);
            }
        }
    });

    btnPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String toSpeak = etTxt.getText().toString();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ttsGreater21(toSpeak);
            } else {
                ttsUnder20(toSpeak);
            }
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();

        }
    });

}
 
Example 14
Source File: MainActivity.java    From reader with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	mTakePhoto = (Button) findViewById(R.id.take_photo);
	mImageView = (ImageView) findViewById(R.id.imageview);
	speak1 = (Button) findViewById(R.id.speak1);
	responseText = (TextView) findViewById(R.id.textView1);

	mTakePhoto.setOnClickListener(this);
	speak1.setOnClickListener(this);
	
	tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
           @Override
           public void onInit(int status) {
               // TODO Auto-generated method stub
               if (status == TextToSpeech.SUCCESS)
               {
                   int result = tts.setLanguage(Locale.CHINA);
                   if (result == TextToSpeech.LANG_MISSING_DATA
                           || result == TextToSpeech.LANG_NOT_SUPPORTED)
                   {
                       Toast.makeText(getApplicationContext(), "Language is not available.",
                               Toast.LENGTH_SHORT).show();
                   }
               } else {
               	Toast.makeText(getApplicationContext(), "Language is available.",
                           Toast.LENGTH_SHORT).show();
               }
           }
       });
}
 
Example 15
Source File: TTS.java    From always-on-amoled with GNU General Public License v3.0 4 votes vote down vote up
public void sayCurrentStatus() {
    tts = new TextToSpeech(context, TTS.this);
    tts.setLanguage(Locale.getDefault());
    tts.speak("", TextToSpeech.QUEUE_FLUSH, null);
}
 
Example 16
Source File: MainService.java    From android-play-games-in-motion with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    // The service is being created.
    Utils.logDebug(TAG, "onCreate");
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_1);
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_2);
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_3);
    registerReceiver(mReceiver, intentFilter);

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // Determines the behavior for handling Audio Focus surrender.
    mAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS) {

                if (mTextToSpeech.isSpeaking()) {
                    mTextToSpeech.setOnUtteranceProgressListener(null);
                    mTextToSpeech.stop();
                }

                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }

                // Abandon Audio Focus, if it's requested elsewhere.
                mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);

                // Restart the current moment if AudioFocus was lost. Since AudioFocus is only
                // requested away from this application if this application was using it,
                // only Moments that play sound will restart in this way.
                if (mMission != null) {
                    mMission.restartMoment();
                }
            }
        }
    };

    // Asynchronously prepares the TextToSpeech.
    mTextToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                // Check if language is available.
                switch (mTextToSpeech.isLanguageAvailable(DEFAULT_TEXT_TO_SPEECH_LOCALE)) {
                    case TextToSpeech.LANG_AVAILABLE:
                    case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                    case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                        Utils.logDebug(TAG, "TTS locale supported.");
                        mTextToSpeech.setLanguage(DEFAULT_TEXT_TO_SPEECH_LOCALE);
                        mIsTextToSpeechReady = true;
                        break;
                    case TextToSpeech.LANG_MISSING_DATA:
                        Utils.logDebug(TAG, "TTS missing data, ask for install.");
                        Intent installIntent = new Intent();
                        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                        startActivity(installIntent);
                        break;
                    default:
                        Utils.logDebug(TAG, "TTS local not supported.");
                        break;
                }
            }
        }
    });

    mMediaPlayer = new MediaPlayer();
}