google语音识别启动方式

1.非Intent方式,没有google语音识别的界面
private void initGoogleSpeechRecognizer() {
    speechGoogle = android.speech.SpeechRecognizer.createSpeechRecognizer(this);
    speechGoogle.setRecognitionListener(recognitionListener);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault().toString());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());

    // only for 6.0
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE<
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android语音机器人代码,使用Android Studio进行开发: 1. 创建一个新的Android Studio项目,命名为"VoiceRobot"。 2. 在build.gradle文件中添加以下依赖项: ``` dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' //Speech Recognition Library implementation 'com.google.android.gms:play-services-speech:15.0.1' } ``` 3. 在activity_main.xml文件中添加以下布局: ``` <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Result" app:layout_constraintBottom_toTopOf="@+id/btn_listen" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btn_listen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Listen" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_result" /> </android.support.constraint.ConstraintLayout> ``` 4. 在MainActivity.java文件中添加以下代码: ``` import android.os.Bundle; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends AppCompatActivity implements RecognitionListener { private SpeechRecognizer speechRecognizer; private Intent recognizerIntent; private TextView tvResult; private Button btnListen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult=findViewById(R.id.tv_result); btnListen=findViewById(R.id.btn_listen); speechRecognizer=SpeechRecognizer.createSpeechRecognizer(this); recognizerIntent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); speechRecognizer.setRecognitionListener(this); btnListen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { speechRecognizer.startListening(recognizerIntent); } }); } @Override public void onReadyForSpeech(Bundle bundle) { } @Override public void onBeginningOfSpeech() { } @Override public void onRmsChanged(float v) { } @Override public void onBufferReceived(byte[] bytes) { } @Override public void onEndOfSpeech() { } @Override public void onError(int i) { } @Override public void onResults(Bundle bundle) { ArrayList<String> matches=bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if(matches!=null) { tvResult.setText(matches.get(0)); } } @Override public void onPartialResults(Bundle bundle) { } @Override public void onEvent(int i, Bundle bundle) { } } ``` 在这个代码中,我们创建了一个SpeechRecognizer对象来处理语音识别。我们使用RecognizerIntent来定义我们想要识别的语音,并使用Locale.getDefault()来设置默认语言。我们使用setRecognitionListener()方法来注册一个RecognitionListener对象,该对象用于接收语音识别结果。我们还创建了一个按钮,当点击时会启动语音识别,并在TextView中显示结果。最后我们实现了RecognitionListener接口,以接收语音识别结果。 这就是一个简单的Android语音机器人代码,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值