class MainActivity : AppCompatActivity(), RecognitionListener {
override fun onReadyForSpeech(params: Bundle?) {
Log.i("cccccccc","onReadyForSpeech===")
}
override fun onRmsChanged(rmsdB: Float) {
Log.i("cccccccc","onRmsChanged===")
}
override fun onBufferReceived(buffer: ByteArray?) {
Log.i("cccccccc","onBufferReceived===")
}
override fun onPartialResults(partialResults: Bundle?) {
Log.i("cccccccc","onPartialResults===")
}
override fun onEvent(eventType: Int, params: Bundle?) {
Log.i("cccccccc","onEvent===")
}
override fun onBeginningOfSpeech() {
Log.i("cccccccc","onBeginningOfSpeech===")
}
override fun onEndOfSpeech() {
Log.i("cccccccc","onEndOfSpeech===")
}
override fun onError(error: Int) {
Log.i("cccccccc","onError===")
}
override fun onResults(results: Bundle?) {
Log.i("cccccccc","==="+results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)?.size.toString())
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// var a = SpeechRecognizer.isRecognitionAvailable(this)
//
//
var mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(this);
// 启动服务需要一个 Intent
var mRecognitionIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognitionIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
mRecognitionIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
// mLocale 是一个语音种类,可以根据自己的需求去设置
mRecognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
// 开始语音识别 结果在 mSpeechRecognizer.setRecognitionListener(this);回调中
mSpeechRecognizer.startListening(mRecognitionIntent);
/*这个是可以使用的*/
// var intent = Intent(
// RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //设置识别模式
// intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please start your voice");//开始说话描述
// try {
// startActivityForResult(intent, 100); //RESULT_SPEECH为Activity回调时的code值
// } catch ( a:Exception) { //当前设备无法支持google语音识别
// var t = Toast . makeText (getApplicationContext(),
// "Opps! Your device doesn't support Speech to Text",
// Toast.LENGTH_SHORT);
// t.show();
// }
//// 停止监听
// mSpeechRecognizer.stopListening();
//
//// 取消服务
// mSpeechRecognizer.cancel();
}
}
网友评论