-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynthesizer.java
83 lines (67 loc) · 2.49 KB
/
Synthesizer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cn.sribd.si.threads;
import cn.sribd.si.Utils;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SynthesizerListener;
public class Synthesizer implements Runnable {
private SynthesizerListener mSynthesizerListener;
private SpeechSynthesizer speechSynthesizer;
private volatile boolean speakCompleted;
public Synthesizer() {
this.mSynthesizerListener = new SynthesizerListener() {
@Override
public void onBufferProgress(int i, int i1, int i2, String s) {
}
@Override
public void onSpeakBegin() {
}
@Override
public void onSpeakProgress(int i, int i1, int i2) {
}
@Override
public void onSpeakPaused() {
}
@Override
public void onSpeakResumed() {
}
@Override
public void onCompleted(SpeechError speechError) {
speakCompleted = true;
}
};
this.speechSynthesizer = SpeechSynthesizer.createSynthesizer();
speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");//设置发音人
speechSynthesizer.setParameter(SpeechConstant.SPEED, "40");//设置语速
speechSynthesizer.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围0~100
speakCompleted = true;
}
@Override
public void run() {
for (; ; ) {
try {
String chineseText;
if (!speakCompleted) {
Thread.sleep(10);
continue;
}
synchronized (Utils.speakBuffer) {
while (Utils.speakBuffer.isEmpty()) {
Utils.speakBuffer.wait();
}
chineseText = Utils.speakBuffer.removeFirst();
}
if (chineseText != null) {
// System.out.println(chineseText);
speakCompleted = false;
speechSynthesizer.startSpeaking(chineseText, mSynthesizerListener);
}
if (Utils.isStopped) {
break;
}
} catch (InterruptedException e) {
break;
}
}
}
}