开通账户
在讯飞开发者平台上注册申请账户,并且创建自己的应用,为自己的应用添加语音合成服务
SDK下载以及导入
- 根据选择的服务,下载官方的SDK
- 将Sample项目中的Msc.jar以及Sunflower.jar放到目录libs下,将资源文件arm64-v8a等文件夹(里面是libmsc.so)放到jniLibs文件夹下
添加权限说明
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
创建MyApplication文件
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化sdk,appid是你创建应用时,科大讯飞给的唯一的id
SpeechUtility.createUtility(MyApplication.this,"appid="+你的APPID);
SpeakerUtil.init(this);
}
}
在AndroidManifest.Xml文件中配置自己的MyApplication
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
···
###创建自己的语音助手类
public class SpeakerUtil {
// 语音合成对象
private static SpeechSynthesizer speechSynthesizer;
// 语记安装助手类
private static ApkInstaller apkInstaller;
/**
*
* 引擎类型:
* 1. 如果是SpeechConstant.TYPE_CLOUD,代表是采用云端语音合成
* 2. 如果是SpeechConstant.TYPE_LOCAL,代表是采用本地语音合成,需要下载一个讯飞语音助手
*
*/
private static final String mEngineType = SpeechConstant.TYPE_CLOUD;
//发音人
private static final String voiceName = "xiaoyan";
/**
* 初始化工作
*
* @param context the context
*/
public static void init(Context context) {
if (speechSynthesizer != null) {
speechSynthesizer.resumeSpeaking();
return;
}
// 初始化合成对象
speechSynthesizer = SpeechSynthesizer.createSynthesizer(context,
mTtsInitListener);
}
/**
* 调用此函数,合成语音
*
* @param activity the activity
* @param text the text
*/
public static void startSpeaking(Activity activity, String text) {
init(activity);
int code = speechSynthesizer.startSpeaking(text, null);
if (code != ErrorCode.SUCCESS) {
if (code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED) {
// 未安装则跳转到提示安装页面
if (apkInstaller == null) {
apkInstaller = new ApkInstaller();
}
apkInstaller.install(activity);
} else {
Log.e("SpeechSynthesizer", "======语音合成失败 code=" + code);
}
}
}
/**
*
* 初始化回调监听
*
**/
private static InitListener mTtsInitListener = new InitListener() {
@Override
public void onInit(int code) {
if (code != ErrorCode.SUCCESS) {
Log.e("SpeechSynthesizer", "======初始化失败,错误码 code=" + code);
} else {
setParam();
}
}
};
/**
*
* 设置云端参数
*
* */
private static void setParam(){
// 清空参数
speechSynthesizer.setParameter(SpeechConstant.PARAMS, null);
// 根据合成引擎设置相应参数
if(mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
speechSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
// 设置在线合成发音人
speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, voiceName);
//设置合成语速
speechSynthesizer.setParameter(SpeechConstant.SPEED, "30");//mSharedPreferences.getString("speed_preference", "50")
//设置合成音调
speechSynthesizer.setParameter(SpeechConstant.PITCH, "50");//mSharedPreferences.getString("pitch_preference", "50")
//设置合成音量
speechSynthesizer.setParameter(SpeechConstant.VOLUME, "50");//mSharedPreferences.getString("volume_preference", "50")
}else {
speechSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
// 设置本地合成发音人 voicer为空,默认通过语记界面指定发音人。
speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "");
//本地合成不设置语速、音调、音量,默认使用语记设置,开发者如需自定义参数,请参考在线合成参数设置
}
//设置播放器音频流类型
speechSynthesizer.setParameter(SpeechConstant.STREAM_TYPE,"3");// mSharedPreferences.getString("stream_preference", "3")
// 设置播放合成音频打断音乐播放,默认为true
speechSynthesizer.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");
// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
// 注:AUDIO_FORMAT参数语记需要更新版本才能生效
speechSynthesizer.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
speechSynthesizer.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/tts.wav");
}
/**
* 销毁掉第一个
*/
public static void onDestroy() {
if (speechSynthesizer == null)
return;
speechSynthesizer.stopSpeaking();
// 退出时释放连接
speechSynthesizer.destroy();
}
本地语音合成和云端语音合成
本地语音合成需要下载讯飞语记才可以用,云端合成不需要
可通过如下变量设置成自己需要的方式
注:云端语音不是很清晰
private static final String mEngineType = SpeechConstant.TYPE_CLOUD;
调用方法
public class MainActivity extends AppCompatActivity {
private TextView btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (TextView)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SpeakerUtil.startSpeaking(MainActivity.this, "欢迎您");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
SpeakerUtil.onDestroy();
}
}
赶紧试一下吧