赞
踩
最近项目要用到语音合成(TTS)的功能,目前国内只有两家:科大讯飞:科大讯飞开发者平台 和 百度:百度语音 。科大讯飞支持完全离线,但是需要付费(成本高),又因为百度虽然目前没有开放纯离线模式下的语音合成SDK,但是开放了离在线融合语音合成SDK,会自动判断当前网络环境,自动匹配使用离线还是在线合成引擎。简言之,就是根据网络的不同状态,实现离线条件下的语音合成。
首先我们还是来确认一下使用百度TTS的流程:
1下载官方sdk。(这个不多说)
2申请apiID,这个是重点,说白了102的错误归根结底出现在这里。我们来看图
这个是百度云平台的语音合成管理https://cloud.baidu.com。
首先语音合成必选,这是你的目的;其次文字识别得选(离线需要这个权限),没有这个受不了权就会出现-102授权失败的问题;然后包名,平台都不用说。
然后就是导包,把官方下载的demo中的data改名assets删掉里面的temp-listenes,放到main平级的目录。
最后就是敲代码了:下面我把这个语音合成给封装了一下:
- /**
- * Created by MaTengBin on 2017/7/13.
- */
-
- public class SpeechUtil implements SpeechSynthesizerListener {
-
- private SpeechSynthesizer mSpeechSynthesizer;
- private String mSampleDirPath;
- private static final String SAMPLE_DIR_NAME = "baiduTTS";
- 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 Context context;
-
- public SpeechUtil(Context context){
- this.context = context;
- initialEnv();
- initialTts();
- }
-
-
- 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 = context.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 initialTts() {
- this.mSpeechSynthesizer = SpeechSynthesizer.getInstance();
- this.mSpeechSynthesizer.setContext(context);
- this.mSpeechSynthesizer.setSpeechSynthesizerListener(this);
- // 文本模型文件路径 (离线引擎使用)
- this.mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, mSampleDirPath + "/"
- + TEXT_MODEL_NAME);
- // 声学模型文件路径 (离线引擎使用)
- this.mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, mSampleDirPath + "/"
- + SPEECH_FEMALE_MODEL_NAME);
- // 本地授权文件路径,如未设置将使用默认路径.设置临时授权文件路径,LICENCE_FILE_NAME请替换成临时授权文件的实际路径,仅在使用临时license文件时需要进行设置,如果在[应用管理]中开通了正式离线授权,不需要设置该参数,建议将该行代码删除(离线引擎)
- // 如果合成结果出现临时授权文件将要到期的提示,说明使用了临时授权文件,请删除临时授权即可。
- // this.mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_LICENCE_FILE, mSampleDirPath + "/"
- // + LICENSE_FILE_NAME);
- // 请替换为语音开发者平台上注册应用得到的App ID (离线授权)
- this.mSpeechSynthesizer.setAppId("9913010"/*这里只是为了让Demo运行使用的APPID,请替换成自己的id。*/);
- // 请替换为语音开发者平台注册应用得到的apikey和secretkey (在线授权)
- this.mSpeechSynthesizer.setApiKey("9iIhf2hjqH975Lhqte5FlmnU",
- "d9db97caef194d5eb5e79900da9613c4"/*这里只是为了让Demo正常运行使用APIKey,请替换成自己的APIKey*/);
- // 发音人(在线引擎),可用参数为0,1,2,3。。。(服务器端会动态增加,各值含义参考文档,以文档说明为准。0--普通女声,1--普通男声,2--特别男声,3--情感男声。。。)
- this.mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
- // 设置Mix模式的合成策略
- this.mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_DEFAULT);
-
-
-
- // 初始化tts
- mSpeechSynthesizer.initTts(TtsMode.MIX);
- // 加载离线英文资源(提供离线英文合成功能)
- mSpeechSynthesizer.loadEnglishModel(mSampleDirPath + "/" + ENGLISH_TEXT_MODEL_NAME, mSampleDirPath
- + "/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME);
- }
- String ago = "";
- public void speak(String s) {
- String text = s.toString();
-
- //需要合成的文本text的长度不能超过1024个GBK字节。
- if (!TextUtils.isEmpty(text) && !ago.equals(text)) {
- mSpeechSynthesizer.speak(text);
- ago = text;
- }
- }
-
-
- @Override
- public void onSynthesizeStart(String s) {
-
- }
-
- @Override
- public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {
- }
-
- @Override
- public void onSynthesizeFinish(String s) {
-
- }
-
- @Override
- public void onSpeechStart(String s) {
-
- }
-
- @Override
- public void onSpeechProgressChanged(String s, int i) {
-
- }
-
- @Override
- public void onSpeechFinish(String s) {
- mSpeechSynthesizer.stop();
- }
-
- @Override
- public void onError(String s, SpeechError speechError) {
-
- }
- }
- SpeechUtil speechUtil = new SpeechUtil(context);
- speechUtil.speak(s);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。