被csdn上的很多人和官方文档坑了一整天,一个bug一直是报空指针。最后终于找到了原因,因为csdn上的人都没有写如何配置JNILibs和配置对应cpu类型的.so库。导致我连初始化也成功不了,debug了一整天。我都有炸了电脑的冲动。废话不多说开始我的表演:
public class MyAPP extends Application {
@Override
public void onCreate() {
super.onCreate();
SpeechUtility.createUtility(MyAPP.this, SpeechConstant.APPID + "=填写自己appid"); //初始化
}
}
4.(我被坑惨了的地方,妈卖批~)
在build.gradle(Module:app)里面配置ndk和jni
defaultConfig {
applicationId "com.jzt.mykdxf"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//配置.so文件!
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'x86', 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
//配置JNILibs
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
5.封装在线语音合成的方法:
/**
* Created by Malik J on 2017/6/5.
*/
/**
* 语音合成的类 发音人明细http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=367
*
* @author kongqw
*/
public class KqwSpeechCompound {
// Log标签
private static final String TAG = "KqwSpeechCompound";
// 上下文
private Context mContext;
// 语音合成对象
private static SpeechSynthesizer mTts;
/**
* 发音人
*/
public final static String[] COLOUD_VOICERS_ENTRIES = {"小燕", "小宇", "凯瑟琳", "亨利", "玛丽", "小研", "小琪", "小峰", "小梅", "小莉", "小蓉", "小芸", "小坤", "小强 ", "小莹",
"小新", "楠楠", "老孙",};
public final static String[] COLOUD_VOICERS_VALUE = {"xiaoyan", "xiaoyu", "catherine", "henry", "vimary", "vixy", "xiaoqi", "vixf", "xiaomei",
"xiaolin", "xiaorong", "xiaoqian", "xiaokun", "xiaoqiang", "vixying", "xiaoxin", "nannan", "vils",};
/**
* 构造方法
*
* @param context
*/
public KqwSpeechCompound(Context context) {
Log.d("tag54", "初始化失败,错ss 误码:" );
// 上下文
mContext = context;
// 初始化合成对象
mTts = SpeechSynthesizer.createSynthesizer(mContext, new InitListener() {
@Override
public void onInit(int code) {
if (code != ErrorCode.SUCCESS) {
Log.d("tag54", "初始化失败,错误码:" + code);
}
Log.d("tag54", "初始化失败,q错误码:" + code);
}
});
}
/**
* 开始合成
*
* @param text
*/
public void speaking(String text) {
// 非空判断
if (TextUtils.isEmpty(text)) {
return;
}
int code = mTts.startSpeaking(text, mTtsListener);
Log.d("tag54","-----"+code+"++++++++++");
if (code != ErrorCode.SUCCESS) {
if (code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED) {
Toast.makeText(mContext, "没有安装语音+ code = " + code, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "语音合成失败,错误码: " + code, Toast.LENGTH_SHORT).show();
}
}
}
/*
* 停止语音播报
*/
public static void stopSpeaking() {
// 对象非空并且正在说话
if (null != mTts && mTts.isSpeaking()) {
// 停止说话
mTts.stopSpeaking();
}
}
/**
* 判断当前有没有说话
*
* @return
*/
public static boolean isSpeaking() {
if (null != mTts) {
return mTts.isSpeaking();
} else {
return false;
}
}
/**
* 合成回调监听。
*/
private SynthesizerListener mTtsListener = new SynthesizerListener() {
@Override
public void onSpeakBegin() {
Log.i(TAG, "开始播放");
}
@Override
public void onSpeakPaused() {
Log.i(TAG, "暂停播放");
}
@Override
public void onSpeakResumed() {
Log.i(TAG, "继续播放");
}
@Override
public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
// TODO 缓冲的进度
Log.i(TAG, "缓冲 : " + percent);
}
@Override
public void onSpeakProgress(int percent, int beginPos, int endPos) {
// TODO 说话的进度
Log.i(TAG, "合成 : " + percent);
}
@Override
public void onCompleted(SpeechError error) {
if (error == null) {
Log.i(TAG, "播放完成");
} else if (error != null) {
Log.i(TAG, error.getPlainDescription(true));
}
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
}
};
/**
* 参数设置
*
* @return
*/
private void setParam() {
// 清空参数
mTts.setParameter(SpeechConstant.PARAMS, null);
// 引擎类型 网络
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
// 设置发音人
mTts.setParameter(SpeechConstant.VOICE_NAME, COLOUD_VOICERS_VALUE[0]);
// 设置语速
mTts.setParameter(SpeechConstant.SPEED, "50");
// 设置音调
mTts.setParameter(SpeechConstant.PITCH, "50");
// 设置音量
mTts.setParameter(SpeechConstant.VOLUME, "100");
// 设置播放器音频流类型
mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
// mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/KRobot/wavaudio.pcm");
// 背景音乐 1有 0 无
// mTts.setParameter("bgs", "1");
}
}
6.调用方法:
public class MainActivity extends AppCompatActivity {
private EditText edt;
private Button btn;
private KqwSpeechCompound kqwSpeechCompound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt= (EditText) findViewById(R.id.et_text);
btn= (Button) findViewById(R.id.btn);
kqwSpeechCompound=new KqwSpeechCompound(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
kqwSpeechCompound.speaking(edt.getText().toString().trim());
}
});
}
}
OK了~ 这样就可以直接实现在线语音啦~