讯飞语音识别 android,讯飞语音识别Android-Demo

淳于祺
2023-12-01

importjava.io.UnsupportedEncodingException;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Environment;importandroid.text.TextUtils;importandroid.util.Log;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;importcom.iflytek.cloud.ErrorCode;importcom.iflytek.cloud.InitListener;importcom.iflytek.cloud.LexiconListener;importcom.iflytek.cloud.RecognizerListener;importcom.iflytek.cloud.RecognizerResult;importcom.iflytek.cloud.SpeechConstant;importcom.iflytek.cloud.SpeechError;importcom.iflytek.cloud.SpeechListener;importcom.iflytek.cloud.SpeechRecognizer;importcom.iflytek.cloud.SpeechUtility;importcom.iflytek.cloud.util.ContactManager.ContactListener;importcom.iflytek.cloud.util.UserWords;public class MainActivity extends Activity implementsOnClickListener {private static String TAG = "IatDemo";//语音听写对象

privateSpeechRecognizer mIat;//听写结果内容

privateEditText mResultText;//用户词表下载结果

private String mDownloadResult = "";privateToast mToast;private intret;privateButton startRecording;private String voiceResult = "";privateTextView text_result;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mIat= SpeechRecognizer.createRecognizer(this, mInitListener);

mToast= Toast.makeText(this, "", Toast.LENGTH_SHORT);

text_result= (TextView) this.findViewById(R.id.text_result);

startRecording= (Button) this.findViewById(R.id.start_recording);

startRecording.setOnClickListener(this);

}

@Overridepublic booleanonCreateOptionsMenu(Menu menu) {//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);return true;

}public voidgetVoice() {

SpeechUtility.createUtility(MainActivity.this, "appid=540593c3");//设置参数

setParam();//不显示听写对话框

ret =mIat.startListening(recognizerListener);

}public voidsetParam() {

mIat.setParameter(SpeechConstant.LANGUAGE,"zh_cn");//设置语音前端点

mIat.setParameter(SpeechConstant.VAD_BOS, "4000");//设置语音后端点

mIat.setParameter(SpeechConstant.VAD_EOS, "1000");//设置标点符号

mIat.setParameter(SpeechConstant.ASR_PTT, "1");//设置音频保存路径

mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/xunfei");

}/*** 初始化监听器。*/

private InitListener mInitListener = newInitListener() {

@Overridepublic void onInit(intcode) {

Log.d(TAG,"SpeechRecognizer init() code = " +code);if (code ==ErrorCode.SUCCESS) {

Toast.makeText(MainActivity.this, "init success", 0).show();

}

}

};/*** 用户词表下载监听器。*/

private SpeechListener downloadlistener = newSpeechListener() {

@Overridepublic void onData(byte[] data) {try{if (data != null && data.length > 1)

mDownloadResult= new String(data, "utf-8");

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

}

@Overridepublic voidonCompleted(SpeechError error) {if (error != null) {

}else if(TextUtils.isEmpty(mDownloadResult)) {

}else{

mResultText.setText("");

UserWords userwords= newUserWords(mDownloadResult.toString());if (userwords == null || userwords.getKeys() == null) {return;

}for(String key : userwords.getKeys()) {

mResultText.append(key+ ":");for(String word : userwords.getWords(key)) {

mResultText.append(word+ ",");

}

mResultText.append("\n");

}

}

}

@Overridepublic void onEvent(inteventType, Bundle params) {

}

};/*** 听写监听器。*/

private RecognizerListener recognizerListener = newRecognizerListener() {

@Overridepublic voidonBeginOfSpeech() {

Toast.makeText(MainActivity.this, "begin speech", 0).show();

}

@Overridepublic voidonError(SpeechError error) {

Toast.makeText(MainActivity.this, error.getErrorDescription(), 0).show();

}

@Overridepublic voidonEndOfSpeech() {

Toast.makeText(MainActivity.this, "end", 0).show();

}

@Overridepublic void onEvent(int eventType, int arg1, intarg2, String msg) {

Toast.makeText(MainActivity.this, "on event", 0).show();

}

@Overridepublic void onResult(RecognizerResult results, booleanisLast) {

String text=JsonParser.parseIatResult(results.getResultString());

voiceResult= voiceResult +text;if(isLast) {

text_result.setText(voiceResult);

}

}

@Overridepublic void onVolumeChanged(intvolume) {

}

};

@Overrideprotected voidonResume() {//SpeechUtility.getUtility().checkServiceInstalled();

super.onResume();

}

@Overridepublic voidonClick(View v) {switch(v.getId()) {caseR.id.start_recording:

getVoice();break;

}

}

}

 类似资料: