开发过程中有时需要用到科大讯飞的SDK,下面是我自己写的文字转语音的工具类:
package utils;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
public class SpeechUiUtils {
//传入上下文,和需要转成语音的文字
public static void read(Context context, String read) {
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(context,
null);
mTts.setParameter(SpeechConstant.VOICE_NAME, “aisxping”);
mTts.setParameter(SpeechConstant.SPEED, “50”);
mTts.setParameter(SpeechConstant.VOLUME, “100”);
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);// 设置云端
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, “./sdcard/iflytek.pcm”);// 语音保存位置
mTts.startSpeaking(read, mSynListener);
}
//传入上下文,和需要转成语音的文字,voice_name代表读文字的指定语音人,注意这个不是随便写,要看科大讯飞的文档
public static void read(Context context, String read,String voice_name) {
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(context,
null);
mTts.setParameter(SpeechConstant.VOICE_NAME, voice_name);
mTts.setParameter(SpeechConstant.SPEED, "50");
mTts.setParameter(SpeechConstant.VOLUME, "100");
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);// 设置云端
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");// 语音保存位置
mTts.startSpeaking(read, mSynListener);
}
//自定义读语音,自己传语音的监听
public static void read(Context context, String read,SynthesizerListener mSynListener) {
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(context,
null);
mTts.setParameter(SpeechConstant.VOICE_NAME, "aisxping");
mTts.setParameter(SpeechConstant.SPEED, "50");
mTts.setParameter(SpeechConstant.VOLUME, "100");
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);// 设置云端
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");// 语音保存位置
mTts.startSpeaking(read, mSynListener);
}
public static void read_load_local(Context context, String read) {
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(context,
null);
mTts.setParameter(SpeechConstant.VOICE_NAME, "aisxping");
mTts.setParameter(SpeechConstant.SPEED, "50");
mTts.setParameter(SpeechConstant.VOLUME, "100");
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);// 设置云端
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,
Environment.getExternalStorageDirectory() + "/iflytek.pcm");// 语音保存位置
mTts.startSpeaking(read, mSynListener);
}
private static SynthesizerListener mSynListener = new SynthesizerListener() {
// 缓冲进度回调
// percent为缓冲进度0~100,beginPos为缓冲音频在文本中开始位置,endPos表示缓冲音频在文本中结束位置,info为附加信息
@Override
public void onBufferProgress(int percent, int beginPos, int endPos,
String info) {
}
// 会话结束回调接口,没有错误时,error为null
@Override
public void onCompleted(SpeechError error) {
}
// 会话事件回调接口
@Override
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
}
// 开始播放
@Override
public void onSpeakBegin() {
}
// 暂停播放
@Override
public void onSpeakPaused() {
}
// 播放进度回调
// percent为播放进度0~100,beginPos为播放音频在文本中开始位置,endPos表示播放音频在文本中结束位置.
@Override
public void onSpeakProgress(int percent, int beginPos, int endPos) {
}
// 恢复播放回调接口
@Override
public void onSpeakResumed() {
}
};
}
本人个人项目地址:百度搜索安卓应用—-点击进入百度应用市场—搜索”电话老人版”