1,注册科大讯飞开发者,新建应用,下载离线语音合成SDK到本地
2,将下载好的SDK中的jar,assets文件下的tts包,.so文件复制到项目中
3,将下面的工具类复制到项目中
public class KqwSpeechSynthesizer { private static final String TAG = "KqwSpeechSynthesizer"; private Context mContext; private SpeechSynthesizer mTts; public KqwSpeechSynthesizer(Context context) { mTts = SpeechSynthesizer.createSynthesizer(context, new InitListener() { public void onInit(int code) { Log.d(TAG, "InitListener init() code = " + code); if (code != ErrorCode.SUCCESS) { Toast.makeText(mContext, "初始化失败,错误码:" + code, Toast.LENGTH_SHORT).show(); public void start(String text) { int code = mTts.startSpeaking(text, mTtsListener); if (code != ErrorCode.SUCCESS) { Toast.makeText(mContext, "语音合成失败,错误码: " + code, Toast.LENGTH_SHORT).show(); private SynthesizerListener mTtsListener = new SynthesizerListener() { public void onSpeakBegin() { public void onSpeakPaused() { public void onSpeakResumed() { public void onBufferProgress(int percent, int beginPos, int endPos, String info) { Log.i(TAG, "传冲进度 :" + percent); public void onSpeakProgress(int percent, int beginPos, int endPos) { // Log.i(TAG, "合成进度 : " + percent); public void onCompleted(SpeechError speechError) { if (speechError == null) { Log.i(TAG, "error : " + speechError.toString()); public void onEvent(int i, int i1, int i2, Bundle bundle) { private void setParam() { mTts.setParameter(SpeechConstant.PARAMS, null); mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL); mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath()); mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); mTts.setParameter(SpeechConstant.SPEED, "50"); mTts.setParameter(SpeechConstant.PITCH, "50"); mTts.setParameter(SpeechConstant.VOLUME, "50"); mTts.setParameter(SpeechConstant.STREAM_TYPE, "3"); private String getResourcePath() { return ResourceUtil.generateResourcePath(mContext, ResourceUtil.RESOURCE_TYPE.assets, "tts/common.jet") + ";" + ResourceUtil.generateResourcePath(mContext, ResourceUtil.RESOURCE_TYPE.assets, "tts/xiaoyan.jet");
4,项目中使用
首先在Application中初始化:
String param = "appid=5c6224f9," + SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC; SpeechUtility.createUtility(this, param); //将这里的appid的值替换成你在第一步中拿到的appid的值
然后在有需要的页面使用:
在onCreate中初始化工具类:mKqwSpeechSynthesizer = new KqwSpeechSynthesizer(this);
在具体要语音播报的地方调用:mKqwSpeechSynthesizer.start(speckContent);
在onDestory中释放:mKqwSpeechSynthesizer.stop();
OK!完成,就是这么简单!
|