sdk下载导入自行参考讯飞官网
初始化sdk
在自定义的Application中初始化
//初始化讯飞语音
SpeechUtility.createUtility(mContext, SpeechConstant.APPID + "=官网生成的APPID");
语音初始化
public class TTSController implements SynthesizerListener {
public static final String TAG = TTSController.class.getSimpleName();
public static TTSController ttsManager;
public static boolean isfinish = true;
private Context mContext;
// 合成对象.
private SpeechSynthesizer mSpeechSynthesizer;
@Override
public void onEvent(int i, int i1, int i2, Bundle bundle) {
}
TTSController(Context context) {
mContext = context;
}
public static TTSController getInstance(Context context) {
if (ttsManager == null) {
ttsManager = new TTSController(context);
}
return ttsManager;
}
public void init() {
// 初始化合成对象.
mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(mContext, null);
initSpeechSynthesizer();
}
/**
* 使用SpeechSynthesizer合成语音,不弹出合成DiaLogger.
*
* @param
*/
public void playText(String playText) {
// if (!isfinish) {
// return;
// }
if (null == mSpeechSynthesizer) {
Logger.i("mSpeechSynthesizer is null");
// 创建合成对象.
mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(mContext, null);
initSpeechSynthesizer();
}
// 进行语音合成.
mSpeechSynthesizer.startSpeaking(playText, this);
}
public void stopSpeaking() {
if (mSpeechSynthesizer != null) mSpeechSynthesizer.stopSpeaking();
}
public void startSpeaking() {
isfinish = true;
}
private void initSpeechSynthesizer() {
// 设置发音人
mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "vixy");
// 设置语速
mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, "55");
// 设置音量
mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME, "tts_volume");
// 设置语调
mSpeechSynthesizer.setParameter(SpeechConstant.PITCH, "tts_pitch");
//设置与其他音频软件冲突的时候是否暂停其他音频
mSpeechSynthesizer.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "false");
}
@Override
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
Logger.d(TAG, "onBufferProgress");
}
@Override
public void onCompleted(SpeechError arg0) {
// Logger.i("error info :" + arg0.getErrorDescription());
//
// Logger.e(TAG, arg0.getErrorDescription());
Logger.d(TAG, "onCompleted");
isfinish = true;
}
@Override
public void onSpeakBegin() {
Logger.i("onSpeakBegin");
Logger.d(TAG, "onSpeakBegin");
isfinish = false;
}
@Override
public void onSpeakPaused() {
Logger.i("onSpeakPaused");
Logger.d(TAG, "onSpeakPaused");
}
@Override
public void onSpeakProgress(int arg0, int arg1, int arg2) {
Logger.i("onSpeakProgress :" + arg0);
}
@Override
public void onSpeakResumed() {
Logger.i("onSpeakResumed ");
}
public void destroy() {
if (mSpeechSynthesizer != null) {
mSpeechSynthesizer.stopSpeaking();
}
}
}
使用方法
//初始化语音设置
TTSController mTtsManager = TTSController.getInstance(getApplicationContext());
mTtsManager.init();
//播放
mTtsManager.playText("1231312");
语音的基本参数设置可参考官网自行选择设置。