離線語音解析
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();
}
}