-
Notifications
You must be signed in to change notification settings - Fork 86
/
TextToSpeechActivity.java
53 lines (43 loc) · 1.69 KB
/
TextToSpeechActivity.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.tensorflow.yolo.view;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import org.tensorflow.yolo.model.Recognition;
import java.util.List;
import java.util.Locale;
import static org.tensorflow.yolo.Config.LOGGING_TAG;
/**
* Created by Zoltan Szabo on 4/25/18.
*/
public abstract class TextToSpeechActivity extends CameraActivity implements TextToSpeech.OnInitListener {
private TextToSpeech textToSpeech;
private String lastRecognizedClass = "";
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e(LOGGING_TAG, "Text to speech error: This Language is not supported");
}
} else {
Log.e(LOGGING_TAG, "Text to speech: Initilization Failed!");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textToSpeech = new TextToSpeech(this, this);
}
protected void speak(List<Recognition> results) {
if (!(results.isEmpty() || lastRecognizedClass.equals(results.get(0).getTitle()))) {
lastRecognizedClass = results.get(0).getTitle();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(lastRecognizedClass, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
textToSpeech.speak(lastRecognizedClass, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}