百度语音合成

下载百度语音合成Sample

1:添加:assests(:离线语音合成需要),libs

2:离线语音 初始化 时间长,最好service中先初始化

public class InitSpeechSynthesizer {
    private Context context;
    private SpeechSynthesizer speechSynthesizer;
    public InitSpeechSynthesizer(Context context,SpeechSynthesizer speechSynthesizer){
        this.context=context;
        this.speechSynthesizer=speechSynthesizer;
        init();
    }

    private void init(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    speechSynthesizer.setContext(context);
                    speechSynthesizer.setSpeechSynthesizerListener(new MySpeechSynthesizer()); //listener是SpeechSynthesizerListener 的实现类,需要实现您自己的业务逻辑。
                    speechSynthesizer.setAppId("11303697");
                    speechSynthesizer.setApiKey("lbLbZmct0UCxNtESOsQqWkvR","2wQdDWiGQCG5i8f0IuNuthGmvPiltjpA");
                    speechSynthesizer.auth(TtsMode.MIX); // 离在线混合
                    //设置网络模式
                    speechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_DEFAULT);
                    speechSynthesizer.setStereoVolume (1.0f, 1.0f);
                    speechSynthesizer.initTts(TtsMode.MIX); // 初始化离在线混合模式,如果只需要在线合成功能,使用 TtsMode.ONLINE
                    OfflineResource offlineResource = new OfflineResource(context,OfflineResource.VOICE_DUXY);  //离线模式
                    speechSynthesizer.loadModel(offlineResource.getModelFilename(),offlineResource.getTextFilename());
                } catch (Exception e) {
                    Log.i("tag",e.toString());
                }
            }
        }).start();
    }

    class OfflineResource {

        public static final String VOICE_FEMALE = "F";

        public static final String VOICE_MALE = "M";


        public static final String VOICE_DUYY = "Y";

        public static final String VOICE_DUXY = "X";

        private static final String SAMPLE_DIR = "baiduTTS";

        private AssetManager assets;
        private String destPath;

        private String textFilename;
        private String modelFilename;

        private HashMap<String, Boolean> mapInitied = new HashMap<String, Boolean>();

        public OfflineResource(Context context, String voiceType) throws IOException {
            context = context.getApplicationContext();
            this.assets = context.getApplicationContext().getAssets();
            this.destPath = new DataHandler(context).getDir();
            setOfflineVoiceType(voiceType);
        }

        public String getModelFilename() {
            return modelFilename;
        }

        public String getTextFilename() {
            return textFilename;
        }

        //assets下 百度 声音文件名
        private void setOfflineVoiceType(String voiceType) throws IOException {
            String text = "bd_etts_text.dat";
            String model;
            if (VOICE_MALE.equals(voiceType)) {
                model = "bd_etts_common_speech_m15_mand_eng_high_am-mix_v3.0.0_20170505.dat";
            } else if (VOICE_FEMALE.equals(voiceType)) {
                model = "bd_etts_common_speech_f7_mand_eng_high_am-mix_v3.0.0_20170512.dat";
            } else if (VOICE_DUXY.equals(voiceType)) {
                model = "bd_etts_common_speech_yyjw_mand_eng_high_am-mix_v3.0.0_20170512.dat";
            } else if (VOICE_DUYY.equals(voiceType)) {
                model = "bd_etts_common_speech_as_mand_eng_high_am_v3.0.0_20170516.dat";
            } else if(SAMPLE_DIR.equals(voiceType)){
                model = "bd_etts_common_speech_as_mand_eng_high_am_v3.0.0_20170516.dat";
            }else{
                throw new RuntimeException("voice type is not in list");
            }
            textFilename = copyAssetsFile(text);
            modelFilename = copyAssetsFile(model);

        }


        private String copyAssetsFile(String sourceFilename) throws IOException {
            String destFilename = destPath + "/" + sourceFilename;
            boolean recover = false;
            Boolean existed = mapInitied.get(sourceFilename); // 启动时完全覆盖一次
            if (existed == null || !existed) {
                recover = true;
            }
            copyFromAssets(assets, sourceFilename, destFilename, recover);
            Log.i(TAG, "文件复制成功:" + destFilename);
            return destFilename;
        }

        private void copyFromAssets(AssetManager assets, String source, String dest, boolean isCover)
                throws IOException {
            File file = new File(dest);
            if (isCover || (!isCover && !file.exists())) {
                InputStream is = null;
                FileOutputStream fos = null;
                try {
                    is = assets.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);
                    }
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } finally {
                            if (is != null) {
                                is.close();
                            }
                        }
                    }
                }
            }
        }

    }

    class MySpeechSynthesizer implements SpeechSynthesizerListener {

        @Override
        public void onSynthesizeStart(String s) {
            Log.i("tag","start");
        }

        @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) {

        }

        @Override
        public void onError(String s, SpeechError speechError) {

        }
    }

}

getDir(); 定义语音输出目录

3:离线语音 初始化 时间长,最好service中先初始化 speechSynthesizer = StudyService.speechSynthesizer;

speeckSynthesizer.speak( string);


猜你喜欢

转载自blog.csdn.net/tangzheng828/article/details/80492658