Android科⼤讯飞在线和离线语⾳听写效果图:
参考资料
项⽬⾥要⽤语⾳听写,想到了科⼤讯飞,参考上⾯的资料完成了最简单的在线有UI的语⾳识别,后⾯想要改成离线也可以使⽤。参考下⾯的⽂章
下载好语记和离线资源之后,想要改成有UI的RecognizerDialog离线语⾳听写,但是没成功,试了⼀下没有UI的SpeechRecognizer,发现可以,只需在设置参数的时候加上⼀句
recognizer.setParameter(SpeechConstant.ENGINE_TYPE,SpeechConstant.TYPE_LOCAL);//调⽤语记接⼝
就可以使⽤了。可能是RecognizerDialog带有UI界⾯,已经默认写成在线的了吧
下⾯只贴出主活动的代码,其他参考上⾯的资料
ample.administrator.voicerecognition;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
public class MainActivity extends AppCompatActivity {
private Button btn_voiceRec,btn_voiceRecOffline;
private RecognizerDialog iatDialog=null;
private SpeechRecognizer recognizer=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
btn_voiceRec = (Button) findViewById(R.id.btn_voiceRec);
btn_voiceRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (iatDialog!=null){
iatDialog.cancel();
iatDialog.destroy();
}
startDialogOnline();
}
});
btn_voiceRecOffline= (Button) findViewById(R.id.btn_voiceRecOffline);
btn_voiceRecOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//                if (recognizer!=null){
//                    recognizer.cancel();
//                    recognizer.destroy();
//                }
startNoDialogOffline();
}
});
});
}
/**
* 初始化。
*/
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d("tag", "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
Log.d("tag", "初始化失败,错误码:" + code);
}
}
};
public void startDialogOnline() {
//1.创建SpeechRecognizer对象,第⼆个参数:本地听写时传InitListener
iatDialog = new RecognizerDialog(this, mInitListener);
//2.设置听写参数
iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin ");
//3.设置回调接⼝
iatDialog.setListener(new RecognizerDialogListener() {
@Override
public void onResult(RecognizerResult recognizerResult, boolean b) {
if (!b) {
String json = ResultString();
String str = JsonParser.parseIatResult(json);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(SpeechError speechError) {
Log.d("error", String());
}
});
//4.开始听写
iatDialog.show();
}
private void startNoDialogOffline(){
//1.创建SpeechRecognizer对象,第⼆个参数:本地听写时传InitListener
recognizer = ateRecognizer(this,null);
//2.设置听写参数
recognizer.setParameter(SpeechConstant.ENGINE_TYPE,SpeechConstant.TYPE_LOCAL);//调⽤语记接⼝
recognizer.setParameter(SpeechConstant.DOMAIN, "iat");//参数设为语⾳听写
recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");//中⽂
recognizer.setParameter(SpeechConstant.ACCENT, "mandarin ");//普通话
//3.设置回调接⼝
recognizer.startListening(new RecognizerListener() {
@Override
public void onVolumeChanged(int i, byte[] bytes) {
}
@Override
public void onBeginOfSpeech() {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onResult(RecognizerResult recognizerResult, boolean isLast) {
if (isLast) {
String json = ResultString();
String str = JsonParser.parseIatResult(json);
Log.d("tag",str);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(SpeechError speechError) {
Log.d("error", String());
}
@Override
public void onEvent(int i, int i1, int i2, Bundle bundle) {
}
});
}
}—————————————————————————————————
2016.11.2更新
离线语⾳做了个简单的UI,效果如图:
完整代码如下,改动部分已注释:
error parse ample.administrator.voicerecognition;
import android.app.ProgressDialog;

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