-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
66 lines (55 loc) · 1.75 KB
/
MainActivity.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
54
55
56
57
58
59
60
61
62
63
64
65
66
package arithene.tts;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
public class MainActivity extends Activity implements OnInitListener {
private TextToSpeech tts;
private Button speakButton;
private EditText inputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
inputText = findViewById(R.id.inputText);
speakButton = findViewById(R.id.speakButton);
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speakOut();
}
});
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakButton.setEnabled(true);
}
} else {
Log.e("TTS", "Initialization Failed!");
}
}
private void speakOut() {
String text = inputText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
@Override
public void onDestroy() {
// Shutdown TTS when activity is destroyed
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}