首先要成为百度开发者,申请应用等一系列的事情,参考官方接入指南
Demo集成参考(http://yuyin.baidu.com/docs/tts/159[官方文档地址])及官方给的demo,这个请自行下载。我本人采用的是离线及在线融合的sdk(官方没有纯离线的sdk)。
因为我用的是android studio所以jar放在lib文件夹下,其他的放在jniLibs下。一下是代码:
public class MainActivity extends AppCompatActivity implements SpeechSynthesizerListener{
// 语音合成客户端
private SpeechSynthesizer mSpeechSynthesizer;
private String mSampleDirPath;
private static final String SAMPLE_DIR_NAME = "VoiceTest";
private static final String SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female.dat";
private static final String SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male.dat";
private static final String TEXT_MODEL_NAME = "bd_etts_text.dat";
private static final String LICENSE_FILE_NAME = "temp_license";
private static final String ENGLISH_SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female_en.dat";
private static final String ENGLISH_SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male_en.dat";
private static final String ENGLISH_TEXT_MODEL_NAME = "bd_etts_text_en.dat";
private LoggerProxy loggerProxy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loggerProxy = new LoggerProxy();
initialEnv();
startTTS();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = mSpeechSynthesizer.speak("骚年,开始飞行吧");
Log.e("Voice", result + "");
}
});
loggerProxy.printable(true);
}
private void initialEnv() {
if (mSampleDirPath == null) {
String sdcardPath = Environment.getExternalStorageDirectory().toString();
mSampleDirPath = sdcardPath + "/" + SAMPLE_DIR_NAME;
}
makeDir(mSampleDirPath);
copyFromAssetsToSdcard(false, SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME);
copyFromAssetsToSdcard(false, SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_MALE_MODEL_NAME);
copyFromAssetsToSdcard(false, TEXT_MODEL_NAME, mSampleDirPath + "/" + TEXT_MODEL_NAME);
copyFromAssetsToSdcard(false, LICENSE_FILE_NAME, mSampleDirPath + "/" + LICENSE_FILE_NAME);
copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/"
+ ENGLISH_SPEECH_FEMALE_MODEL_NAME);
copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/"
+ ENGLISH_SPEECH_MALE_MODEL_NAME);
copyFromAssetsToSdcard(false, "english/" + ENGLISH_TEXT_MODEL_NAME, mSampleDirPath + "/"
+ ENGLISH_TEXT_MODEL_NAME);
}
private void makeDir(String dirPath) {
File file = new File(dirPath);
if (!file.exists()) {
file.mkdirs();
}
}
/**
* 将sample工程需要的资源文件拷贝到SD卡中使用(授权文件为临时授权文件,请注册正式授权)
*
* @param isCover 是否覆盖已存在的目标文件
* @param source
* @param dest
*/
private void copyFromAssetsToSdcard(boolean isCover, String source, String dest) {
File file = new File(dest);
if (isCover || (!isCover && !file.exists())) {
InputStream is = null;
FileOutputStream fos = null;
try {
is = getResources().getAssets().open(source);
String path = dest;
fos = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int size = 0;
while ((size = is.read(buffer, 0, 1024)) >= 0) {
fos.write(buffer, 0, size);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 初始化语音合成客户端并启动
private void startTTS() {
// 获取语音合成对象实例
mSpeechSynthesizer = SpeechSynthesizer.getInstance();
// 设置context
mSpeechSynthesizer.setContext(this);
// 设置语音合成状态监听器
mSpeechSynthesizer.setSpeechSynthesizerListener(this);
// 设置在线语音合成授权,需要填入从百度语音官网申请的api_key和secret_key
mSpeechSynthesizer.setApiKey("hlwP1QNBqLmBHLHkZBz85DPB", "60b9b7007a0077a6aede427b2b4ad298");
// 设置离线语音合成授权,需要填入从百度语音官网申请的app_id
mSpeechSynthesizer.setAppId("8317228");
//设置网络模式
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_DEFAULT);
// 设置语音合成文本模型文件
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, mSampleDirPath + "/" + TEXT_MODEL_NAME);
// 设置语音合成声音模型文件
mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME);
// 设置语音合成声音授权文件
// mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_LICENCE_FILE, mSampleDirPath + "/" + LICENSE_FILE_NAME);
// 获取语音合成授权信息
AuthInfo authInfo = mSpeechSynthesizer.auth(TtsMode.MIX);
// 判断授权信息是否正确,如果正确则初始化语音合成器并开始语音合成,如果失败则做错误处理
if (authInfo.isSuccess()) {
mSpeechSynthesizer.initTts(TtsMode.MIX);
mSpeechSynthesizer.speak("百度语音正在合成");
} else {
// 授权失败
Log.e("Voice", authInfo.getTtsError().getDetailMessage());
}
}
@Override
public void onSynthesizeStart(String s) {
// 监听到合成开始,在此添加相关操作
}
@Override
public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {
// 监听到有合成数据到达,在此添加相关操作
}
@Override
public void onSynthesizeFinish(String s) {
// 监听到合成结束,在此添加相关操作
Log.e("Voice", s);
}
@Override
public void onSpeechStart(String s) {
// 监听到合成并播放开始,在此添加相关操作
}
@Override
public void onSpeechProgressChanged(String s, int i) {
// 监听到播放进度有变化,在此添加相关操作
}
@Override
public void onSpeechFinish(String s) {
// 监听到播放结束,在此添加相关操作
}
@Override
public void onError(String s, SpeechError speechError) {
// 监听到出错,在此添加相关操作
}
}
以上就是所有的了,下面是遇到的问题。
- 首先你要确保.so库加载正确,该添加的都添加完了
- 其次开通权限的时候需要正确的填写你的应用包名(这个可以修改)
- appid appkey appsecret等填写正确(确保是同一包名)
- 虽然需要离线功能,但是第一次还是需要联网的,它需要下载授权文件,把项目里的授权文件删掉,同时注释掉设置授权文件的那一句代码,代码中已经注释掉了
如果有其他的百度jar包并且集成时报错,删除掉galaxy.jar试试
最后运行demo能够正常播放语音了。
注:很少写博客,如果有不对的地方欢迎批评。还有讯飞语音及其他的sdk也可以实现,不过讯飞语音离线sdk收费,所以就没有研究。有需要的也可以自己去研究。