Skip to content

Commit

Permalink
text to speech now has also callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
gotev committed Aug 30, 2016
1 parent c7d7977 commit e05fdc7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.gotev.speech.Speech;
import net.gotev.speech.SpeechDelegate;
import net.gotev.speech.SpeechRecognitionNotAvailable;
import net.gotev.speech.TextToSpeechCallback;
import net.gotev.toyproject.R;

import java.util.List;
Expand Down Expand Up @@ -55,14 +56,30 @@ private void onButtonClick() {

private void onRecordAudioPermissionGranted() {
try {
Speech.getInstance().stopTextToSpeech();
Speech.getInstance().startListening(MainActivity.this);
} catch (SpeechRecognitionNotAvailable exc) {
Toast.makeText(this, "Speech recognition is not available on this device!", Toast.LENGTH_LONG).show();
}
}

private void onSpeakClick() {
Speech.getInstance().say(getString(R.string.demo_tts));
Speech.getInstance().say(getString(R.string.demo_tts), new TextToSpeechCallback() {
@Override
public void onStart() {
Toast.makeText(MainActivity.this, "onStart", Toast.LENGTH_SHORT).show();
}

@Override
public void onCompleted() {
Toast.makeText(MainActivity.this, "onCompleted", Toast.LENGTH_SHORT).show();
}

@Override
public void onError() {
Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
}
});
}

@Override
Expand All @@ -75,10 +92,13 @@ public void onSpeechResult(String result) {
button.setText(getString(R.string.start_listening));
Log.i(getClass().getSimpleName(), "onSpeechResult");
text.setText("Result: " + result);
if (result.isEmpty())

if (result.isEmpty()) {
Speech.getInstance().say("Ripeti per favore");
else

} else {
Speech.getInstance().say(result);
}
}

@Override
Expand Down
74 changes: 72 additions & 2 deletions speech/src/main/java/net/gotev/speech/Speech.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

/**
* Helper class to easily work with Android speech recognition.
Expand All @@ -39,6 +44,7 @@ public class Speech {
private Context mContext;

private TextToSpeech mTextToSpeech;
private Map<String, TextToSpeechCallback> mTtsCallbacks = new HashMap<>();
private Locale mLocale = Locale.getDefault();
private float mTtsRate = 1.0f;
private float mTtsPitch = 1.0f;
Expand All @@ -65,6 +71,52 @@ public void onInit(int status) {
}
};

private UtteranceProgressListener mTtsProgressListener = new UtteranceProgressListener() {
@Override
public void onStart(final String utteranceId) {
final TextToSpeechCallback callback = mTtsCallbacks.get(utteranceId);

if (callback != null) {
new Handler(mContext.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onStart();
}
});
}
}

@Override
public void onDone(final String utteranceId) {
final TextToSpeechCallback callback = mTtsCallbacks.get(utteranceId);

if (callback != null) {
new Handler(mContext.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onCompleted();
mTtsCallbacks.remove(utteranceId);
}
});
}
}

@Override
public void onError(final String utteranceId) {
final TextToSpeechCallback callback = mTtsCallbacks.get(utteranceId);

if (callback != null) {
new Handler(mContext.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onError();
mTtsCallbacks.remove(utteranceId);
}
});
}
}
};

private RecognitionListener mListener = new RecognitionListener() {

@Override
Expand Down Expand Up @@ -167,6 +219,7 @@ private void commonInitializer(Context context) {

if (mTextToSpeech == null) {
mTextToSpeech = new TextToSpeech(context, mTttsInitListener);
mTextToSpeech.setOnUtteranceProgressListener(mTtsProgressListener);
}

if (SpeechRecognizer.isRecognitionAvailable(context)) {
Expand Down Expand Up @@ -362,14 +415,31 @@ public boolean isListening() {
* @param message message to play
*/
public void say(String message) {
say(message, null);
}

/**
* Uses text to speech to transform a written message into a sound.
* @param message message to play
* @param callback callback which will receive progress status of the operation
*/
public void say(String message, TextToSpeechCallback callback) {
mTextToSpeech.setLanguage(mLocale);
mTextToSpeech.setPitch(mTtsPitch);
mTextToSpeech.setSpeechRate(mTtsRate);

String utteranceId = UUID.randomUUID().toString();

if (callback != null) {
mTtsCallbacks.put(utteranceId, callback);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTextToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null, null);
mTextToSpeech.speak(message, TextToSpeech.QUEUE_ADD, null, utteranceId);
} else {
mTextToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null);
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
mTextToSpeech.speak(message, TextToSpeech.QUEUE_ADD, params);
}
}

Expand Down
12 changes: 12 additions & 0 deletions speech/src/main/java/net/gotev/speech/TextToSpeechCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.gotev.speech;

/**
* Contains the methods which are called to notify text to speech progress status.
*
* @author Aleksandar Gotev
*/
public interface TextToSpeechCallback {
void onStart();
void onCompleted();
void onError();
}

0 comments on commit e05fdc7

Please sign in to comment.