lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.zhaoyang.driver.base.gaode.gpsnav.util;
 
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.os.Bundle;
 
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
 
/**
 * 创建时间:2017-9-20 下午3:35:23
 * 项目名称:IndexActivity
 *
 * @author liqi
 * @version 1.0
 * @since JDK 1.6.0_21
 * 文件名称:MscTTS.java
 * 类说明: 科大讯飞播报类
 */
public class IFlyTTS implements TTS, SynthesizerListener, OnAudioFocusChangeListener {
    private static IFlyTTS iflyTTS = null;
    Context mContext = null;
    private boolean isPlaying = false;
    private AudioManager mAm = null;
    /**
     * todo 请替换您自己申请的ID。
     */
    private final String appId = "c314fc99";
 
    public static IFlyTTS getInstance(Context context) {
        if (iflyTTS == null) {
            iflyTTS = new IFlyTTS(context);
        }
        return iflyTTS;
    }
 
    private IFlyTTS(Context context) {
        mContext = context;
        SpeechUtility.createUtility(mContext, SpeechConstant.APPID + "="
                + appId);
        createSynthesizer();
        mAm = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    }
 
    private SpeechSynthesizer mTts;
 
    public void init() {
        //设置发音人
        if (mTts == null){
            createSynthesizer();
        }
//        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
//        //设置语速,值范围:[0, 100],默认值:50
//        mTts.setParameter(SpeechConstant.SPEED, "55");
//        //设置音量
//        mTts.setParameter(SpeechConstant.VOLUME, "tts_volume");
//        //设置语调
//        mTts.setParameter(SpeechConstant.PITCH, "tts_pitch");
//        //设置与其他音频软件冲突的时候是否暂停其他音频
//        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "false");
//        //女生仅vixy支持多音字播报
//        mTts.setParameter(SpeechConstant.VOICE_NAME, "vixy");
    }
 
 
 
    private void createSynthesizer() {
//        Toast.makeText(mContext, "创建银屏", Toast.LENGTH_SHORT).show();
        mTts = SpeechSynthesizer.createSynthesizer(mContext,
                new InitListener() {
                    @Override
                    public void onInit(int errorcode) {
                        if (ErrorCode.SUCCESS == errorcode) {
                            //初始化成功
//                            Toast.makeText(mContext, "初始化音频成功", Toast.LENGTH_SHORT).show();
                        }
                    }
 
                });
    }
 
    public void playText(String playText) {
        //多音字处理举例
        if (playText != null && playText.contains("京藏")) {
            playText = playText.replace("京藏", "京藏[=zang4]");
        }
        if (playText != null && playText.length() > 0) {
            int result = mAm.requestAudioFocus(this,
                    AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                mTts.startSpeaking(playText, this);
                isPlaying = true;
            }
        }
    }
 
    public void stopSpeak() {
        if (mTts != null) {
            mTts.stopSpeaking();
        }
        isPlaying = false;
    }
 
    public void destroy() {
        stopSpeak();
        if (mTts != null) {
            mTts.destroy();
        }
        iflyTTS = null;
    }
 
    @Override
    public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
    }
 
    //播报是否成功以及错误码
    //在音频播放完成,或会话出现错误时,将回调此函数。若为null,则没有出现错误。
    @Override
    public void onCompleted(SpeechError arg0) {
        isPlaying = false;
        if (mAm != null) {
            mAm.abandonAudioFocus(this);
        }
        if (callBack != null) {
            if (arg0 == null) {
                callBack.onCompleted(0);
            }
        }
    }
 
    @Override
    public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
    }
 
    @Override
    public void onSpeakBegin() {
        isPlaying = true;
    }
 
    @Override
    public void onSpeakPaused() {
        isPlaying = false;
    }
 
    @Override
    public void onSpeakProgress(int arg0, int arg1, int arg2) {
    }
 
    @Override
    public void onSpeakResumed() {
    }
 
 
    @Override
    public boolean isPlaying() {
        return isPlaying;
    }
 
 
    ICallBack callBack = null;
 
    @Override
    public void setCallback(ICallBack callback) {
        callBack = callback;
    }
 
    @Override
    public void onAudioFocusChange(int focusChange) {
        // TODO Auto-generated method stub
 
    }
}