-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speaker.java
58 lines (46 loc) · 1.36 KB
/
Speaker.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
/*
* Автор сия безобразия - github.com/EugeneFitzrberg/SmsSender
*
* */
package com.example.sms_watcher;
import android.content.Context;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import java.util.HashMap;
import java.util.Locale;
public class Speaker implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private boolean ready = false;
private boolean allowed = false;
public Speaker(Context context){
tts = new TextToSpeech(context,this);
}
public boolean isAllowed(){
return allowed;
}
public void allow(boolean flag){
this.allowed = flag;
}
@Override
public void onInit(int i) {
if(i == TextToSpeech.SUCCESS){
tts.setLanguage(Locale.ENGLISH);
ready = true;
}else{
ready = false;
}
}
public void speak(String text){
if(ready && allowed){
HashMap<String,String> hashMap = new HashMap<String,String>();
hashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
tts.speak(text,TextToSpeech.QUEUE_ADD,hashMap);
}
}
public void pause(int duration){
tts.playSilence(duration,TextToSpeech.QUEUE_ADD,null);
}
public void destroy(){
tts.shutdown();
}
}