语音识别处理器

Speech Recognition Handler

这是我第一次在这里发帖。

我是 Android Studio 的新手。

我想为 google 语音识别创建一个处理程序。

这是主要的class

package com.example.recnonocimientodevoz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Index extends Activity {

    private Button boton;
    private EditText texto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        boton = (Button)findViewById(R.id.boton);
        texto = (EditText) findViewById(R.id.texto);
        boton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                VRecognition VR = new VRecognition();
                VR.startRecognition();
                texto.setText(VR.getResult());
            }
        });
    }
}

这是处理程序

package com.example.recnonocimientodevoz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import java.util.ArrayList;

public class VRecognition extends Activity{

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1;
    private String phrase = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
    }

    public VRecognition(){
    }

    public void startRecognition (){
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){
            ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            phrase = result.get(0);
        }
    }
    public String getResult(){
        return phrase;
    }
}

我调试了代码,发现调用onActivityResult时崩溃了。

希望你能帮助我。谢谢

这是我的代码,这里是 connected 方法,用于检查网络连接,没有那个语音识别将无法工作...

您还应该像这样在 android 清单中使用权限

AndroidManifest.xml
<uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

然后代码

    public class SpeechRecognition extends Activity {
        private static final int REQUEST_CODE = 1234;
        Button Start;
        TextView Speech;
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Start = (Button)findViewById(R.id.start_reg);
            Speech = (TextView)findViewById(R.id.speech);

            Start.setOnClickListener(new OnClickListener() {
            @Override
             public void onClick(View v) {
                if(isConnected()){
                 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                 startActivityForResult(intent, REQUEST_CODE);
                         }
                else{
                    Toast.makeText(getApplicationContext(), "Plese Connect to Internet", Toast.LENGTH_LONG).show();
                }}

            });   
    }


    public  boolean isConnected()
        { 
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo net = cm.getActiveNetworkInfo();
        if (net!=null && net.isAvailable() && net.isConnected()) {
            return true;
        } else {
            return false; 
        }
        }


     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
             String text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speech.setText(text);
}

谢谢, 问候 Jayakar V

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boton = (Button)findViewById(R.id.button);
    texto = (EditText) findViewById(R.id.editText);
    boton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent i;
            i = new Intent(getApplicationContext(),VRecognition.class);
            startActivity(i);
        }
    });
}

并将 VRecognition Activity 更改为

public class VRecognition extends Activity{
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1;
private String phrase = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    startRecognition();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){
        ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        phrase = result.get(0);
    }
}


public void startRecognition(){
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

}

}