接上讯飞语音识别(听写)
https://blog.csdn.net/nishigesb123/article/details/90478104
依旧是讯飞...接着上一个做下去了省的再集成一次
参考文档:https://doc.xfyun.cn/msc_android/%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90.html
多的就不讲了
语音合成
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_content"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="语音合成"
android:onClick="startClick"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
代码
基本上就是配置一下参数....建议参考Demo来配,或者读API
http://mscdoc.xfyun.cn/android/api/
语音合成中,主要参数包括:
- 语言(LANGUAGE,中文、英文等)
- 方言(ACCENT,中文的普通话,粤语等)
- 发音人特征(性别,年龄,语气)
- 语速(SPEED)
- 音量(VOLUME)
- 语调(PITCH)
- 音频采样率(SAMPLE_RATE)
package com.example.a5_23speech_recognition;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.util.ResourceUtil;
public class Main2Activity extends AppCompatActivity {
private EditText editText_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editText_content = findViewById(R.id.editText_content);
}
public void startClick(View view) {
//创建对象
SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null);
//合成参数
//朗读者列表可以参考
// https://doc.xfyun.cn/msc_android/%E5%90%88%E6%88%90%E5%8F%91%E9%9F%B3%E4%BA%BA%E5%88%97%E8%A1%A8.html
mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
//在线模式
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
//设置合成语速
mTts.setParameter(SpeechConstant.SPEED, "50");
//设置合成音调
mTts.setParameter(SpeechConstant.PITCH, "50");
//设置合成音量
mTts.setParameter(SpeechConstant.VOLUME, "50");
// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
/* mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "pcm");
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/tts.pcm");*/
//离线模式
/* if( SpeechConstant.TYPE_LOCAL.equals(engineType)
&&SpeechConstant.MODE_MSC.equals(engineMode) ){
// 需下载使用对应的离线合成SDK
mTts.setParameter( ResourceUtil.TTS_RES_PATH, ttsResPath );
}*/
String content = editText_content.getText().toString();
mTts.startSpeaking(content, mSynListener);
}
//合成***(导带cloud字样的包
private SynthesizerListener mSynListener = new SynthesizerListener() {
@Override
public void onSpeakBegin() {
//开始
}
@Override
public void onBufferProgress(int i, int i1, int i2, String s) {
}
@Override
public void onSpeakPaused() {
}
@Override
public void onSpeakResumed() {
}
@Override
public void onSpeakProgress(int i, int i1, int i2) {
//可以在这里设置,来实现显示朗读进度
}
@Override
public void onCompleted(SpeechError speechError) {
}
@Override
public void onEvent(int i, int i1, int i2, Bundle bundle) {
}
};
}
效果
好吧截图是截不出来效果的,输入内容点按钮反正是能听到声音就对了← ←