zoukankan      html  css  js  c++  java
  • Android 云之声离线语音合成

    离线语音解析

    public class SpeechUtilOffline implements TTSPlayerListener {
        public static final String appKey = "_appKey_";
        private ITTSControl mTTSPlayer;
        private Context context;
        private SoundUtil soundUtil;
        public SpeechUtilOffline(Context context) {
            this.context = context;
            init();
        }
    
        /**
         * 初始化引擎
         * @author JPH
         * @date 2015-4-14 下午7:32:58
         */
        private void init() {
            soundUtil=new SoundUtil(context);
            mTTSPlayer = TTSFactory.createTTSControl(context, appKey);// 初始化语音合成对象
            mTTSPlayer.setTTSListener(this);// 设置回调监听
            mTTSPlayer.setStreamType(AudioManager.STREAM_MUSIC);//设置音频流
            mTTSPlayer.setVoiceSpeed(0.5f);//设置播报语速,播报语速,数值范围 0.1~2.5 默认为 1.0
            mTTSPlayer.setVoicePitch(0.9f);//设置播报音高,调节音高,数值范围 0.9~1.1 默认为 1.0
            mTTSPlayer.init();// 初始化合成引擎
        }
        /**
         * 停止播放
         */
        public void stop(){
            mTTSPlayer.stop();
        }
    
        /**
         * 播放
         */
        public void play(String content) {
            mTTSPlayer.play(content);
        }
    
        /**
         * 释放资源
         */
        public void release() {
            mTTSPlayer.release();
        }
    
        @Override
        public void onPlayEnd() {
            // 播放完成回调
            soundUtil.playSound(1);
            Log.i("msg", "onPlayEnd");
        }
    
        @Override
        public void onPlayBegin() {
            // 开始播放回调
            Log.i("msg", "onPlayBegin");
        }
    
        @Override
        public void onInitFinish() {
            // 初始化成功回调
            Log.i("msg", "onInitFinish");
        }
    
        @Override
        public void onError(cn.yunzhisheng.tts.offline.common.USCError arg0) {
            // 语音合成错误回调
            Log.i("msg", "onError");
        }
    
        @Override
        public void onCancel() {
            // 取消播放回调
            Log.i("msg", "onCancel");
        }
    
        @Override
        public void onBuffer() {
            // 开始缓冲回调
            Log.i("msg", "onBuffer");
    
        }
    }

     云之声离线Jar包和so文件下载地址

     下载地址

     Android SoundPool语音播放类

    public class SoundUtil {
        private static HashMap<Integer, Integer> musicId;
        private static SoundPool mSoundPool;
        public SoundUtil(Context context){
            mSoundPool=new SoundPool(12, 0,5);
            musicId= new HashMap<>();
            musicId.put(1, mSoundPool.load(context, R.raw.tsc_success, 1));
            musicId.put(2, mSoundPool.load(context, R.raw.check_failure, 1));
        }
        //播放
        public void playSound(int redId) {
            //音频资源 左声道音量 右声道音量 优先级 循环播放次数 回放速度:该值在0.5-2.0之间 1为正常速度
            mSoundPool.play(musicId.get(redId),1,1, 0, 0, 1);
        }
        //暂停
        public void pause(int redId){
            mSoundPool.pause(redId);
        }
        //重新开始
        public void resume(int redId){
            mSoundPool.resume(redId);
        }
        //停止
        public void stop(int redId){
            mSoundPool.stop(redId);
        }
        //移除一个音频
        public void unload(int redId){
            mSoundPool.unload(redId);
        }
        //释放所有音频资源
        public void release(){
            mSoundPool.release();
        }
    }
  • 相关阅读:
    Redis慢查询日志
    Docker:容器
    CentOS7.X中使用yum安装nginx
    Nginx简介及配置详解(二)
    Nginx简介及配置详解(一)
    Redis持久化策略
    golang学习笔记——异常
    golang学习笔记——面向对象(接口)
    golang学习笔记——面向对象(方法)
    golang学习笔记——struct结构体
  • 原文地址:https://www.cnblogs.com/94xiyang/p/9376711.html
Copyright © 2011-2022 走看看