云知声和科大讯飞语音合成封装

2018-06-26  本文已影响0人  程序员阿兵

项目中需要对语音合成,采用离线语音合成,起初使用的是云知声的三方SDK后来换成了科大讯飞。对此我将两种方案封装了一下,使用者可以直接拿来使用。

1 云知声

/**
 * Created by GuiYanBing on 2018/3/28 11:05
 * E-Mail Address:guiyanbing@zhiyihealth.com.cn
 *//*

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import com.unisound.client.SpeechConstants;
import com.unisound.client.SpeechSynthesizer;
import com.unisound.client.SpeechSynthesizerListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.com.zhiyihealth.s1.Ui.activity.AppAplication;

*/
/**
 * Created by GuiYanBing on 2018/3/28.
 *//*


public class TTSUtils implements SpeechSynthesizerListener {
    private static final String TAG = "TTSUtils";
    private static volatile TTSUtils instance = null;
    private boolean isInitSuccess = false;
    private SpeechSynthesizer mTTSPlayer;
    private static final String SAMPLE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/unisound/tts/";
    private final String mFrontendModel= "/sdcard/unisound/tts/frontend_model";
    private static final String FRONTEND_MODEL = "frontend_model";
    private static final String BACKEND_MODEL = "backend_lzl";
    private static final String APPKEY = "xgiqe5lrtlfnzepqwd24sc3eafa4kisdd7y3xnqi";
    private static final String SECRET = "b322f60233c4b27d3a42e0a44af8ba5c";

    private TTSUtils() {
    }
    public static TTSUtils getInstance() {
        if (instance == null) {
            synchronized (TTSUtils.class) {
                if (instance == null) {
                    instance = new TTSUtils();
                }
            }
        }
        return instance;
    }

    public void init() {
        Context context = AppAplication.getInstance();
        mTTSPlayer = new SpeechSynthesizer(context, APPKEY, SECRET);
        // 设置本地合成
        mTTSPlayer.setOption(SpeechConstants.TTS_SERVICE_MODE, SpeechConstants.TTS_SERVICE_MODE_LOCAL);
        File file = new File(SAMPLE_DIR);
        if (!file.exists()) {
            file.mkdirs();
        }
        File _FrontendModelFile = new File(SAMPLE_DIR + FRONTEND_MODEL);
        if (!_FrontendModelFile.exists()) {
            copyAssetsFile2SDCard(context, FRONTEND_MODEL, SAMPLE_DIR + FRONTEND_MODEL);
        }
        File _BackendModelFile = new File(SAMPLE_DIR + BACKEND_MODEL);
        if (!_BackendModelFile.exists()) {
            copyAssetsFile2SDCard(context, BACKEND_MODEL, SAMPLE_DIR + BACKEND_MODEL);
        }
        // 设置前端模型
        mTTSPlayer.setOption(SpeechConstants.TTS_KEY_FRONTEND_MODEL_PATH, SAMPLE_DIR + FRONTEND_MODEL);
        // 设置后端模型
        mTTSPlayer.setOption(SpeechConstants.TTS_KEY_BACKEND_MODEL_PATH, SAMPLE_DIR + BACKEND_MODEL);
        mTTSPlayer.setOption(SpeechConstants.TTS_KEY_VOICE_NAME,"xiaoli");

    */
/*
        设置合成语速 SpeechConstants.TTS_KEY_VOICE_SPEED 范围 0 ~ 100 int
        设置合成音高 SpeechConstants.TTS_KEY_VOICE_PITCH 范围 0 ~ 100 int
        设置合成音量 SpeechConstants.TTS_KEY_VOICE_VOLUME 范围 0 ~ 100 int
        设置合成角色 SpeechConstants.TTS_KEY_VOICE_NAME 只支持xiaoli和xiaolijie String
        设置服务器地址 SpeechConstants.TTS_KEY_SERVER_ADDR 例如:"192.168.0.13:8080" String
        设置合成采样率 SpeechConstants.TTS_KEY_SAMPLE_RATE 例如:16 * 1000 int
        设置合成音频流类型 SpeechConstants.TTS_KEY_STREAM_TYPE 例如:AudioManager.STREAM_MUSIC int
        设置播放开始缓冲时间 SpeechConstants.TTS_KEY_PLAY_START_BUFFER_TIME 例如:0 ~ 500 单位ms int
        设置合成模式 SpeechConstants.TTS_SERVICE_MODE 例如:SpeechConstants.TTS_SERVICE_MODE_LOCAL int
        设置是否打印日志 SpeechConstants.TTS_KEY_IS_DEBUG boolean
        设置是否将英文按拼音读 SpeechConstants.TTS_KEY_IS_READ_ENLISH_IN_PINYIN 如:wang->王 boolean
        设置语音开始段的静音时长 SpeechConstants.TTS_KEY_FRONT_SILENCE 0 ~ 1000 单位ms int
        设置语音结尾段的静音时长 SpeechConstants.TTS_KEY_BACK_SILENCE 0 ~ 1000 单位ms int
        设置是否将线程优先级设为urgentAudio SpeechConstants.TTS_KEY_IS_URGENT_AUDIO boolean
    *//*

        // 设置合成语速
        mTTSPlayer.setOption(SpeechConstants.TTS_KEY_VOICE_SPEED, 50);
        // 设置合成音高
        mTTSPlayer.setOption(SpeechConstants.TTS_KEY_VOICE_PITCH, 50);
        // 设置合成音量
        mTTSPlayer.setOption(SpeechConstants.TTS_KEY_VOICE_VOLUME, 100);
        // 设置回调监听
        mTTSPlayer.setTTSListener(this);
        // 初始化合成引擎
        mTTSPlayer.init(null);
    }

    public void speak(String msg) {
        if (isInitSuccess) {
            mTTSPlayer.playText(msg);
        }else {
            init();
        }
    }

    public void stop() {
        mTTSPlayer.stop();
    }

    public void pause() {
        mTTSPlayer.pause();
    }

    public void resume() {
        mTTSPlayer.resume();
    }

    public void release() {
        if (null != mTTSPlayer) {
            // 释放离线引擎
            mTTSPlayer.release(SpeechConstants.TTS_RELEASE_ENGINE, null);
        }
    }

    @Override
    public void onEvent(int type) {
        switch (type) {
            case SpeechConstants.TTS_EVENT_INIT:
                isInitSuccess = true;
                break;
            case SpeechConstants.TTS_EVENT_SYNTHESIZER_START:
                // 开始合成回调
                Log.i(TAG, "beginSynthesizer");
                break;
            case SpeechConstants.TTS_EVENT_SYNTHESIZER_END:
                // 合成结束回调
                Log.i(TAG, "endSynthesizer");
                break;
            case SpeechConstants.TTS_EVENT_BUFFER_BEGIN:
                // 开始缓存回调
                Log.i(TAG, "beginBuffer");
                break;
            case SpeechConstants.TTS_EVENT_BUFFER_READY:
                // 缓存完毕回调
                Log.i(TAG, "bufferReady");
                break;
            case SpeechConstants.TTS_EVENT_PLAYING_START:
                // 开始播放回调
                Log.i(TAG, "onPlayBegin");
                break;
            case SpeechConstants.TTS_EVENT_PLAYING_END:
                // 播放完成回调
                Log.i(TAG, "onPlayEnd");
                break;
            case SpeechConstants.TTS_EVENT_PAUSE:
                // 暂停回调
                Log.i(TAG, "pause");
                break;
            case SpeechConstants.TTS_EVENT_RESUME:
                // 恢复回调
                Log.i(TAG, "resume");
                break;
            case SpeechConstants.TTS_EVENT_STOP:
                // 停止回调
                Log.i(TAG, "stop");
                break;
            case SpeechConstants.TTS_EVENT_RELEASE:
                // 释放资源回调
                Log.i(TAG, "release");
                break;
            default:
                break;
        }
    }

    @Override
    public void onError(int type, String errorMSG) {
        Log.e(TAG, "语音合成错误回调: " + errorMSG);
    }

    public static void copyAssetsFile2SDCard(Context context, String fileName, String path) {
        try {
            InputStream is = context.getAssets().open(fileName);
            FileOutputStream fos = new FileOutputStream(new File(path));
            byte[] buffer = new byte[1024];
            int byteCount = 0;
            // 循环从输入流读取buffer字节
            while ((byteCount = is.read(buffer)) != -1) {
                // 将读取的输入流写入到输出流
                fos.write(buffer, 0, byteCount);
            }
            fos.flush();// 刷新缓冲区
            is.close();
            fos.close();
        } catch (IOException e) {
            Log.e(TAG, "copyAssetsFile2SDCard: " + e.toString());
        }
    }

}*/

2.科大讯飞语音合成

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.util.ResourceUtil;

import cn.com.zhiyihealth.s1.Ui.activity.LoginActivity;

import static com.growingio.android.sdk.utils.ThreadUtils.runOnUiThread;

/**
 * Created by GuiYanBing on 2018/6/21 20:49
 * E-Mail Address:guiyanbing@zhiyihealth.com.cn
 */

public class TtsKflyUtil {
    private Context mContext;
    private static volatile TtsKflyUtil instance = null;
    private SpeechSynthesizer mTts;
    public static TtsKflyUtil getInstance() {
        if (instance == null) {
            synchronized (TtsKflyUtil.class) {
                if (instance == null) {
                    instance = new TtsKflyUtil();
                }
            }
        }
        return instance;
    }

    // 初始化合成对象
    public void initTts(Context context) {
        mContext=context;
        mTts = SpeechSynthesizer.createSynthesizer(context, mTtsInitListener);
    }
    
    //播报语音

    // 初始化合成对象
    public void sepeakVoice(String voiceString) {
        setParam();
        initLayout(voiceString);
    }
    
    /**
     * 初始化Layout。 语音播报 开始播报
     */
    private void initLayout(final String message) {
        int code = mTts.startSpeaking(message, mTtsListener);
        if (code != ErrorCode.SUCCESS) {
            showTip("语音合成失败,错误码: " + code);
        }
    }

    /**
     * 初始化监听。
     */
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            if (code != ErrorCode.SUCCESS) {
                DesityUtil.showToast(mContext, "初始化失败,错误码:" + code);

            }
        }
    };
    /**
     * 合成回调监听。
     */
    private SynthesizerListener mTtsListener = new SynthesizerListener() {

        @Override
        public void onSpeakBegin() {
            showTip("开始播放");
        }

        @Override
        public void onSpeakPaused() {
            showTip("暂停播放");
        }

        @Override
        public void onSpeakResumed() {
            showTip("继续播放");
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {
            // 合成进度
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
            // 播放进度
        }

        @Override
        public void onCompleted(SpeechError error) {
            if (error == null) {

                showTip("播放完成");
            } else if (error != null) {
                showTip(error.getPlainDescription(true));
            }
        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {

        }
    };
    /**
     * 语音播放完成时监听
     */
    private void showTip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (str == "播放完成") {
                    //第一次播放以后监听
                }
            }
        });
    }

    /**
     * 参数设置
     *
     * @param
     * @return
     */
    private void setParam() {
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
        //设置发音人资源路径
        mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
        //设置发音人
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
        //设置合成语速
        mTts.setParameter(SpeechConstant.SPEED, "30");
        //设置合成音调
        mTts.setParameter(SpeechConstant.PITCH, "50");
        //设置合成音量
        mTts.setParameter(SpeechConstant.VOLUME, "100");
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");

        // 设置播放合成音频打断音乐播放,默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        // 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注:AUDIO_FORMAT参数语记需要更新版本才能生效
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/msc/tts.wav");
    }

    /**
     * 获取发音人资源路径
     *
     * @param
     * @return
     */
    private String getResourcePath() {
        StringBuffer tempBuffer = new StringBuffer();
        //合成通用资源
        tempBuffer.append(ResourceUtil.generateResourcePath(mContext, ResourceUtil.RESOURCE_TYPE.assets, "tts/common.jet"));
        tempBuffer.append(";");
        //发音人资源
        tempBuffer.append(ResourceUtil.generateResourcePath(mContext, ResourceUtil.RESOURCE_TYPE.assets, "tts/" + "xiaoyan" + ".jet"));
        return tempBuffer.toString();
    }


}

上一篇下一篇

猜你喜欢

热点阅读