Skip to content

Instantly share code, notes, and snippets.

@kcochibili
Created May 20, 2017 05:24
Show Gist options
  • Save kcochibili/700fbad9041939d0485628431cffefd1 to your computer and use it in GitHub Desktop.
Save kcochibili/700fbad9041939d0485628431cffefd1 to your computer and use it in GitHub Desktop.
TextToSpeech sample Java
package com.ducky.tempo;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TalkActivity extends Activity {
Button talkButton;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talk);
talkButton = (Button) findViewById(R.id.talk_button);
setListeners(); // call the method that registers alll my button clicks
}
public void setListeners(){
talkButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO make button say "I am a girl"
Context context = getApplicationContext();
TextToSpeech tts = new TextToSpeech(context, onInit); // initialise TheTextToSpeech
tts.setLanguage(Locale.US); // set language
tts.speak("I am a girl", TextToSpeech.QUEUE_ADD, null); // speak
}
});
}
OnInitListener onInit = new OnInitListener() {
@Override
public void onInit(int status) {
// TODO there is nothing to do for when the TextToSpeechHas loaded
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment