Android文字转语音引擎(TTS)讯飞语音+的使用经验分享
Android文字转语音引擎(TTS)讯飞语音+的使用经验分享
/** * 初期化监听。 */ private InitListener mTtsInitListener = new InitListener() { @Override public void onInit(ISpeechModule arg0, int code) { if (code == ErrorCode.SUCCESS) { mTts.startSpeaking("TTS语音合成引擎初始化成功。", mTtsListener); } } };
这是我2015就开始使用的文字转语音引擎TTS的实践,这是免费并且是离线的。后来语音+就收费了,白嫖了这么多年,为了表示感谢,特此分享出来。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initVoiceService();
}
private void initVoiceService() {
if (checkSpeechServiceInstall()) {
// 引擎初始化
SpeechUtility.getUtility(this).setAppid("53c776aa");
mTts = new SpeechSynthesizer(this, mTtsInitListener);
} else mTts = null;
}
这里的引擎初始化好像并不需要连接英特网向讯飞的平台发送“Appid”,我这个"53c776aa"的APPID是我当年注册的。
public void SpeakVoice(String text) {
if (mTts != null && !isPlaySound) {
mTts.startSpeaking(text, mTtsListener);
} else {
ttsList.add(text);
}
}
/**
* 初期化监听。
*/
private InitListener mTtsInitListener = new InitListener() {
@Override
public void onInit(ISpeechModule arg0, int code) {
if (code == ErrorCode.SUCCESS) {
mTts.startSpeaking("TTS语音合成引擎初始化成功。", mTtsListener);
}
}
};
/**
* 合成回调监听。
*/
private SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
@Override
public void onBufferProgress(int progress) throws RemoteException {
}
@Override
public void onCompleted(int code) throws RemoteException {
isPlaySound = false;
if (ttsList.size() > 0) {
String text = ttsList.get(0);
getInstance().SpeakVoice(text);
ttsList.remove(0);
}
}
@Override
public void onSpeakBegin() throws RemoteException {
isPlaySound = true;
}
@Override
public void onSpeakPaused() throws RemoteException {
}
@Override
public void onSpeakProgress(int progress) throws RemoteException {
}
@Override
public void onSpeakResumed() throws RemoteException {
}
};
/**
* 检测科大讯飞语音+引擎是否安装
*
* @return
*/
private boolean checkSpeechServiceInstall() {
String packageName = "com.iflytek.speechcloud";
List<PackageInfo> packages = getPackageManager()
.getInstalledPackages(0);
for (int i = 0; i < packages.size(); i++) {
PackageInfo packageInfo = packages.get(i);
if (packageInfo.packageName.equals(packageName)) {
return true;
} else {
continue;
}
}
return false;
}
原来低版本的安卓是有权限能安装应用的所以语音引擎的APK安装程序是放在RAW里,程序自动进行安装。现在需要自己安装了。附上下载链接:
百度网盘下载地址 (https://pan.baidu.com/s/1bxcai3Quk5HoPRy2-a5b2g) 密码:3si0 科大讯飞语音引擎3.0.apk
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/18255529
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/