Skip to content

Instantly share code, notes, and snippets.

@AraujoJordan
Created April 30, 2016 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AraujoJordan/2c4ee0c60fc69adbd93a1d5d96281d89 to your computer and use it in GitHub Desktop.
Save AraujoJordan/2c4ee0c60fc69adbd93a1d5d96281d89 to your computer and use it in GitHub Desktop.
[Nova Cursos] Aula9 - TTS
package araujo.jordan.a7minnova.activities;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.HashMap;
import java.util.Locale;
import araujo.jordan.a7minnova.R;
public class ExerciseActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private static final int MY_DATA_CHECK_CODE = 111;
private TextToSpeech mTts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this, this);
mTts.setLanguage(Locale.getDefault());
start();
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
private void start() {
String text = "Vamos começar nosso exercício de abdominal. Vamos lá! Em 3, 2, 1. 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13," +
"14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 e 30! Muito bem!";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ttsGreater21(text);
} else {
ttsUnder20(text);
}
mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
}
@Override
public void onDone(String utteranceId) {
finish();
}
@Override
public void onError(String utteranceId) {
}
});
}
@SuppressWarnings("deprecation")
private void ttsUnder20(String text) {
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
String utteranceId=this.hashCode() + "";
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}
@Override
public void onInit(int status) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment