Android 使用讯飞语音 SDK
private String voicer = "xiaoyan"; // 默认发音人
private String[] cloudVoicersEntries;
private String[] cloudVoicersValue;
private int mPercentForBuffering = 0; // 缓冲进度
private int mPercentForPlaying = 0; // 播放进度
private RadioGroup mRadioGroup; // 云端/本地单选按钮
private String mEngineType = SpeechConstant.TYPE_CLOUD; // 引擎类型
// 语音+安装助手类
ApkInstaller mInstaller;
private Toast mToast;
private SharedPreferences mSharedPreferences;
private String content = "你好";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
if (extras != null) {
content = extras.getString("content");
}
// 初始化合成对象
mTts = SpeechSynthesizer.createSynthesizer(this, mTtsInitListener);
mSharedPreferences = getSharedPreferences(TtsSettings.PREFER_NAME,
MODE_PRIVATE);
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
// mInstaller = new ApkInstaller(SpeakService.this);
// 设置参数
setParam();
int code = mTts.startSpeaking(content, mTtsListener);
if (code != ErrorCode.SUCCESS) {
if (code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED) {
// 未安装则跳转到提示安装页面 mInstaller.install();
} else {
// 语音合成失败,错误码:code
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
}
/**
初始化监听。
*/
private InitListener mTtsInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "InitListener init() code = " + code);
if (code != ErrorCode.SUCCESS) {
// 初始化失败,错误码: code
} else {
// 初始化成功,之后可以调用 startSpeaking 方法
// 注:有的开发者在 onCreate 方法中创建完合成对象之后马上就调用 startSpeaking 进行合成,
// 正确的做法是将 onCreate 中的 startSpeaking 调用移至这里
}
}
};
/**
合成回调监听。
*/
private SynthesizerListener mTtsListener = new SynthesizerListener() {
@Override
public void onSpeakBegin() {
// 开始播放
}
@Override
public void onSpeakPaused() {
// 暂停播放
}
@Override
public void onSpeakResumed() {
// 继续播放
}
@Override
public void onBufferProgress(int percent, int beginPos, int endPos,
String info) {
// 合成进度
mPercentForBuffering = percent;
}
@Override
public void onSpeakProgress(int percent, int beginPos, int endPos) {
// 播放进度
mPercentForPlaying = percent;
}
@Override
public void onCompleted(SpeechError error) {
if (error == null) {
// 播放完成
stopSelf();
} else if (error != null) {
}
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
}
};
private void showTip(final String str) {
mToast.setText(str);
mToast.show();
}
private void setParam() {
// 清空参数
mTts.setParameter(SpeechConstant.PARAMS, null);
// 设置合成
if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
mTts.setParameter(SpeechConstant.ENGINE_TYPE,
SpeechConstant.TYPE_CLOUD);
// 设置发音人
mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);
// 设置语速
mTts.setParameter(SpeechConstant.SPEED,
mSharedPreferences.getString("speed_preference", "50"));
// 设置音调
mTts.setParameter(SpeechConstant.PITCH,
mSharedPreferences.getString("pitch_preference", "50"));
// 设置音量
mTts.setParameter(SpeechCons
tant.VOLUME,
mSharedPreferences.getString("volume_preference", "50"));
// 设置播放器音频流类型
mTts.setParameter(SpeechConstant.STREAM_TYPE,
mSharedPreferences.getString("stream_preference", "3"));
} else {
mTts.setParameter(SpeechConstant.ENGINE_TYPE,
SpeechConstant.TYPE_LOCAL);
// 设置发音人 voicer 为空默认通过语音+界面指定发音人。
mTts.setParameter(SpeechConstant.VOICE_NAME, "");
}
}
}
2.离线合成
private String mEngineType = SpeechConstant.TYPE_LOCAL;
3.语义理解
// 设置参数
setParam();
if (mSpeechUnderstander.isUnderstanding()) { // 开始前检查状态
mSpeechUnderstander.stopUnderstanding();
// 停止录音
} else {
ret = mSpeechUnderstander
.startUnderstanding(mRecognizerListener);
if (ret != 0) {
// 语义理解失败,错误码:ret
} else {
showTip(getString(R.string.text_begin));
}
}
3.1 最近一周天气 JSON 解析
String jsonString = result.getResultString();
if (!TextUtils.isEmpty(jsonString)) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonString);
JSONArray mJSONArray = jsonObject.getJSONObject("data")
.getJSONArray("result");
for (int i = 0; i < 7; i++) {
JSONObject jsonDay = mJSONArray.getJSONObject(i);
String tempRange = jsonDay.getString("tempRange"); // 31℃~26℃
String tempArray[] = tempRange.split("~");
editor.putString("postTime",
jsonDay.getString("lastUpdateTime"));
editor.putString("day" + i + "air",
jsonDay.getString("airQuality"));
editor.putString("day" + i + "weather",
jsonDay.getString("weather"));
editor.putString("day" + i + "tmpHigh",
tempArray[0]);
editor.putString("day" + i + "tmpLow", tempArray[1]);
editor.putString(
"day" + i + "wind",
评论