如何用android中的google-speech-recognition来识别一个单词

时间:2016-07-07 12:01:41

标签: android speech-recognition

  

logcat的

    E/AndroidRuntime: FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{phone_finder.maxsoft.com.whereismyphone/PACKAGE.SpeechRecognizerResult}: java.lang.InstantiationException: can't instantiate class phone_finder.maxsoft.com.whereismyphone.SpeechRecognizerResult
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
         at android.app.ActivityThread.access$700(ActivityThread.java:140)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:4921)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:511)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
         at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.InstantiationException: can't instantiate class phone_finder.maxsoft.com.whereismyphone.SpeechRecognizerResult
         at java.lang.Class.newInstanceImpl(Native Method)
         at java.lang.Class.newInstance(Class.java:1319)
         at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
         at android.app.ActivityThread.access$700(ActivityThread.java:140) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
         at android.os.Handler.dispatchMessage(Handler.java:99) 
         at android.os.Looper.loop(Looper.java:137) 
         at android.app.ActivityThread.main(ActivityThread.java:4921) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
         at dalvik.system.NativeStart.main(Native Method) 

我的应用会识别单词,例如。移动之后它会做点什么。我发现GoogleSpeechRecognizer很简单。如下所示:

public abstract class SpeechRecognizerResult extends Activity implements SpeechRecognizerManager.OnResultListener {
private final String TAG = getClass().getSimpleName();
private SpeechRecognizerManager mSpeechRecognizerManager;
private TextView txt_result;

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

    mSpeechRecognizerManager =new SpeechRecognizerManager(this);
    mSpeechRecognizerManager.setOnResultListner(this);

    txt_result= (TextView) findViewById(R.id.text_result);
}

@Override
public void OnResult(ArrayList<String> commands) {

    for(String command:commands) {
        if (command.equals("mobile")){
            Toast.makeText(this, "You said:" + command, Toast.LENGTH_SHORT).show();
            txt_result.setText(command);
            return;
        }

    }
}
}

public class SpeechRecognizerManager {
private Context mContext;
protected android.speech.SpeechRecognizer mGoogleSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
private OnResultListener mOnResultListener;


public SpeechRecognizerManager(Context context) {
    this.mContext = context;
    initGoogleSpeechRecognizer();

}


private void initGoogleSpeechRecognizer() {

    mGoogleSpeechRecognizer = android.speech.SpeechRecognizer
            .createSpeechRecognizer(mContext);

    mGoogleSpeechRecognizer.setRecognitionListener(new GoogleRecognitionListener());

    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES, true);
}


public void destroy() {
    if (mGoogleSpeechRecognizer != null) {
        mGoogleSpeechRecognizer.cancel();
        ;
        mGoogleSpeechRecognizer.destroy();
    }

}



protected class GoogleRecognitionListener implements
        android.speech.RecognitionListener {

    private final String TAG = GoogleRecognitionListener.class
            .getSimpleName();

    @Override
    public void onBeginningOfSpeech() {
    }

    @Override
    public void onEndOfSpeech() {
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
    }

    @Override
    public void onRmsChanged(float rmsdB) {
    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onError(int error) {
        Log.e(TAG, "onError:" + error);

    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d(TAG, "onPartialResultsheard:");

    }

    @Override
    public void onResults(Bundle results) {
        if ((results != null)
                && results
                .containsKey(android.speech.SpeechRecognizer.RESULTS_RECOGNITION)) {
            ArrayList<String> heard = results
                    .getStringArrayList(android.speech.SpeechRecognizer.RESULTS_RECOGNITION);
            float[] scores = results
                    .getFloatArray(android.speech.SpeechRecognizer.CONFIDENCE_SCORES);

            for (int i = 0; i < heard.size(); i++) {
                Log.d(TAG, "onResultsheard:" + heard.get(i)
                        + " confidence:" + scores[i]);

            }


            //send list of words to activity
            if (mOnResultListener!=null){
                mOnResultListener.OnResult(heard);
            }

        }



    }


    @Override
    public void onEvent(int eventType, Bundle params) {

    }

}



    public void setOnResultListner(OnResultListener onResultListener){
        mOnResultListener=onResultListener;
    }

public interface OnResultListener{
    public void OnResult(ArrayList<String> commands);
}
  • 我该怎么做才能解决?
  • 如何继续识别(找到第一个后)?

0 个答案:

没有答案