Android开发讯飞语音步骤

来源:互联网 发布:淘宝上怎么删除评价 编辑:程序博客网 时间:2024/06/07 18:28

一、讯飞开发者平台

进入讯飞开发者平台—点击SDK下载(讯飞SDK均是按照对应的应用下载不同的SDK包,配置不同的秘钥)


选择组合服务SDK下载:

选择相应的服务——选择相应的平台——选择相应的应用——下载


会有所属应用的APPid生成


二:使用语音服务(在线语音合成)

首先定义:

SpeechSynthesizer mTts;

private static final String APPID = "=123456778";

设置相关:

private SynthesizerListener mSynListener = new SynthesizerListener(){        //会话结束回调接口,没有错误时,error为null        //缓冲进度回调        //percent为缓冲进度0~100,beginPos为缓冲音频在文本中开始位置,endPos表示缓冲        //文本中结束位置,info为附加信息。        public void onBufferProgress(int percent, int beginPos, int endPos, String info) {}        //开始播放        public void onSpeakBegin() {}        //暂停播放        public void onSpeakPaused() {}        //播放进度回调        //percent为播放进度0~100,beginPos为播放音频在文本中开始位置,endPos表示播放音        //本中结束位置.        public void onSpeakProgress(int percent, int beginPos, int endPos) {}        //恢复播放回调接口        public void onSpeakResumed() {}        //会话事件回调接口        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {}        public void onCompleted(SpeechError arg0) {            // TODO 自动生成的方法存根        }    };

初始化:

        SpeechUtility.createUtility(HomeActivity.this, SpeechConstant.APPID+ APPID);        mTts= SpeechSynthesizer.createSynthesizer(HomeActivity.this, null);        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); //设置发音人        mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速        mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围 0~100        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);

开始语音播报:

mTts.startSpeaking(qcontent, mSynListener);
其中qcontent为播报的内容。


还得加入相应的类:

import java.util.Locale;import android.content.Context;import android.speech.tts.TextToSpeech;public class SpeechUtils {    private Context context;    private static final String TAG = "SpeechUtils";    private static SpeechUtils singleton;    private TextToSpeech textToSpeech; // TTS对象    public static SpeechUtils getInstance(Context context) {        if (singleton == null) {            synchronized (SpeechUtils.class) {                if (singleton == null) {                    singleton = new SpeechUtils(context);                }            }        }        return singleton;    }    public SpeechUtils(Context context) {        this.context = context;        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {            @Override            public void onInit(int i) {                if (i == TextToSpeech.SUCCESS) {                    textToSpeech.setLanguage(Locale.US);                    textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规                    textToSpeech.setSpeechRate(1.0f);                }            }        });    }    public void speakText(String text) {        if (textToSpeech != null) {            textToSpeech.speak(text,                    TextToSpeech.QUEUE_FLUSH, null);        }    }}



原创粉丝点击