Skip to content

Instantly share code, notes, and snippets.

@dutchand
Last active December 29, 2016 07:16
Show Gist options
  • Save dutchand/ead04c5646b22b6ff9a8 to your computer and use it in GitHub Desktop.
Save dutchand/ead04c5646b22b6ff9a8 to your computer and use it in GitHub Desktop.
Android: Text to speech implementation
package com.ttsl;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import java.util.Locale;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class Speech {
private TextToSpeech tts;
private Context context;
public String tablettext ;
public Speech(Context c){
context=c;
tablettext="Hello";
}
//final String toSpeak
protected void say(final String toSpeak){
tts=new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
if(ttsl.current_language.equals("english")){
tts.setLanguage(Locale.US);
}
else
{
Locale loc = new Locale ("spa", "ESP");
tts.setLanguage(loc);
}
}
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
}
);
}
public void setText(String s)
{
tablettext = s;
}
public String getText()
{
return tablettext;
}
//@Override
public void onPause(){
if(tts !=null){
tts.stop();
tts.shutdown();
}
// super.onPause();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment