Androidstudio通过科⼤讯飞的SDK实现⽂字转语⾳功能
Android studio通过科⼤讯飞的SDK实现⽂字转语⾳功能
⼀、介绍
可以先看看我之前的博客:
这篇博客主要是记录在Android studio上如何通过科⼤讯飞的SDK实现⽂字转语⾳功能。
⼆、具体操作
1.⾸先需要在安卓中配置SDK以及相关权限等内容,由于之前语⾳转⽂字已经配置好了,所以不想要重新进⾏配置,配置⽅法可参考:
2.新建Java类MyAPP,代码如下:
application;
import android.app.Application;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
public class MyAPP extends Application {
@Override
public void onCreate(){
}
}
3.新建Java类KqwSpeechCompound,代码如下:
application;
t.Context;
import android.os.Bundle;
TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
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 = ateSynthesizer(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, PlainDescription(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, ExternalStorageDirectory() + "/KRobot/wavaudio.pcm");        // 背景⾳乐  1有 0 ⽆
androidsdk安装步骤// mTts.setParameter("bgs", "1");
}
}
4.在MainActivity.java中进⾏调⽤:
//合成语⾳
kqwSpeechCompound=new KqwSpeechCompound(this);
kqwSpeechCompound.im());//resultcontent是⼀个字符串
综合之前的代码,MainActivity.java的全部代码如下:
application;
import androidx.appcompat.app.AppCompatActivity;
t.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.iflytek.cloud.RecognizerResult;
import application.XunFeiUtil.parseIatResult;
import application.XunFeiUtil.*;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_click;
private EditText mResultText;
private SQLiteDatabase readableDatabase;
private KqwSpeechCompound kqwSpeechCompound;
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
//相当于建⽴连接
MySqlhelp mySqlhelp = new MySqlhelp(this,"database.db",null,1);
//打开相当于数据库创建完成
readableDatabase = ReadableDatabase();
String sql ="create table  if not exists things(name varchar(50) primary key ,content varchar(500))";
//插⼊⼏条数据
//        sql ="insert into things values('⽔杯','⽔杯在客厅的桌⼦上')";
//        SQL(sql);
//        sql ="insert into things values('钥匙','钥匙在客厅的桌⼦上')";
/
/        SQL(sql);
//        sql ="insert into things values('⿏标','⿏标在卧室的桌⼦上')";
//        SQL(sql);
//调⽤语⾳识别函数
initXunFei(this);
btn_click =(Button) findViewById(R.id.btn_click);
mResultText =((EditText) findViewById(sult));
//点击按钮进⾏识别
btn_click.setOnClickListener(this);
//合成语⾳
kqwSpeechCompound=new KqwSpeechCompound(this);
}
@Override
public void onClick(View v){
startVoice(this, new XunFeiCallbackListener(){
@Override
public void onFinish(RecognizerResult results){
String text = ResultString());
// 获取语⾳转换成的⽂字
mResultText.append(text);
//调⽤python代码
initPython();// 初始化Python环境
Python py = Instance();
PyObject Module("tiquguanjianci").callAttr("main",text);
List returnList =pyObject.asList();//python代码返回的是list
int size = returnList.size();
String resultcontent="";//记录数据库查结果
for(int i = 0; i < size; i++){
String name= (i).toString();//遍历每⼀个关键词
Log.d("res",name);
//查询数据库
String[] selectionArgs = new  String[]{ name };
Cursor cursor = readableDatabase.query("things", null, "name=?", selectionArgs, null, null, null);
if(cursor!=null){
veToNext()){
String content= ColumnIndex("content"));//得到查询结果
resultcontent+=content;
}
cursor.close();
}
}
if(resultcontent=="")
{
resultcontent="抱歉,没有到";
Log.d("res1","没有到");
}
else
{
Log.d("res1",resultcontent);
}
kqwSpeechCompound.im());
}
});
}
// 初始化Python环境

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。