-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
63 lines (54 loc) · 2.02 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
package com.drac.text_to_speech;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
EditText editText;
Button button;
TextToSpeech textToSpeech;
int code = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
if (text.length() > 0) {
textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
});
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, code);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == code) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
textToSpeech = new TextToSpeech(this, this);
} else {
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(intent);
}
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}