Skip to content

Instantly share code, notes, and snippets.

@muzafferyilmaz
Last active December 10, 2021 00:38
Show Gist options
  • Save muzafferyilmaz/aa8e2146ff3aff70bb9db85d46356322 to your computer and use it in GitHub Desktop.
Save muzafferyilmaz/aa8e2146ff3aff70bb9db85d46356322 to your computer and use it in GitHub Desktop.
Android TextToSpeech Manager
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import java.util.HashMap;
import java.util.Locale;
/**
* Created by Muzaffer Yılmaz on 24.04.2018.
*/
public class TtsManager implements TextToSpeech.OnInitListener {
private final String[] mTexts;
private final HashMap<String, String> mTtsMap;
private final TextToSpeech mTts;
private final TextSpeechProgressListener mProgressListener;
private int mTextProgress = 0;
private boolean mIsPlaying;
public interface TextSpeechProgressListener {
void onProgressChanged(int current, int max);
}
public TtsManager(Context context, String text, String splitRegex, @Nullable TextSpeechProgressListener listener) {
mTexts = text.split(splitRegex);
mProgressListener = listener;
mTtsMap = new HashMap<>();
mTtsMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "TTS_ID");
mTts = new TextToSpeech(context, this);
mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
if (!mIsPlaying || mTextProgress == mTexts.length) return;
++mTextProgress;
updateProgress(mTextProgress, mTexts.length);
speakWord();
}
@Override
public void onStart(String utteranceId) {}
@Override
public void onError(String utteranceId) {}
});
}
private void speakWord() {
if (mTextProgress >= mTexts.length) return;
mTts.speak(mTexts[mTextProgress], TextToSpeech.QUEUE_FLUSH, mTtsMap);
}
public void start() {
mIsPlaying = true;
speakWord();
}
public void pause() {
mIsPlaying = false;
mTts.stop();
updateProgress(mTextProgress, mTexts.length);
}
public void stop() {
mIsPlaying = false;
mTts.stop();
mTextProgress = 0;
updateProgress(mTextProgress, mTexts.length);
}
private void updateProgress(int current, int max) {
if (mProgressListener == null) return;
mProgressListener.onProgressChanged(current, max);
}
public void changeProgress(int progress) {
mTextProgress = progress;
// If it's not playing, just change progress
if (!mIsPlaying) return;
// Otherwise start from given progress
pause();
start();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.ROOT);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
} else {
Log.e("TTS", "Initialization Failed!");
}
}
}
TtsManager ttsManager = new TtsManager(context, description, "\\.", new TtsManager.TextSpeechProgressListener() {
@Override
public void onProgressChanged(int current, int max) {
seekBar.setProgress(current);
seekBar.setMax(max);
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mTtsManager == null) return;
mTtsManager.changeProgress(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
ttsManager.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment