SpeechUtil.java
3.01 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.huaheng.mes.util;
import android.content.Context;
import android.os.RemoteException;
import com.iflytek.speech.ErrorCode;
import com.iflytek.speech.ISpeechModule;
import com.iflytek.speech.InitListener;
import com.iflytek.speech.SpeechConstant;
import com.iflytek.speech.SpeechSynthesizer;
import com.iflytek.speech.SynthesizerListener;
public class SpeechUtil {
private SpeechSynthesizer mTts;
private Context mContext;
private static SpeechUtil instance;
public SpeechUtil(Context context) {
mContext = context;
initSpeech();
}
public static SpeechUtil getInstance(Context context) {
if(instance == null) {
instance = new SpeechUtil(context);
}
return instance;
}
/**
* 初期化监听。
*/
private InitListener mTtsInitListener = new InitListener() {
@Override
public void onInit(ISpeechModule arg0, int code) {
MESlog.d("InitListener init() code = " + code);
if (code == ErrorCode.SUCCESS) {
MESlog.d("onInit SUCCESS");
// speeking2();
}
}
};
/**
* 合成回调监听。
*/
private SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
@Override
public void onBufferProgress(int progress) throws RemoteException {
}
@Override
public void onCompleted(int code) throws RemoteException {
MESlog.d("onCompleted code =" + code);
}
@Override
public void onSpeakBegin() throws RemoteException {
MESlog.d( "onSpeakBegin");
}
@Override
public void onSpeakPaused() throws RemoteException {
MESlog.d( "onSpeakPaused.");
}
@Override
public void onSpeakProgress(int progress) throws RemoteException {
}
@Override
public void onSpeakResumed() throws RemoteException {
MESlog.d( "onSpeakResumed.");
}
};
public void initSpeech() {
mTts = new SpeechSynthesizer(mContext, mTtsInitListener);
mTts.setParameter(SpeechConstant.ENGINE_TYPE, "local");
mTts.setParameter(SpeechSynthesizer.VOICE_NAME, "xiaoyan");
mTts.setParameter(SpeechSynthesizer.SPEED, "50");
mTts.setParameter(SpeechSynthesizer.PITCH, "50");
mTts.setParameter(SpeechSynthesizer.VOLUME, "30");
}
public void releaseSpeechSynthesizer() {
if (mTts != null) {
mTts.stopSpeaking(mTtsListener);
// 退出时释放连接
mTts.destory();
}
}
/**
* 根据内容发声
*
* @param speekingContent
*/
public void speech(String speekingContent) {
if (speekingContent == null || speekingContent.length() == 0) {
return;
}
MESlog.d("bizAppNotifyHandle speekingContent:" + speekingContent);
// 播放,code,为还有多少个子为合成
mTts.startSpeaking(speekingContent, mTtsListener);
}
}