2013年4月10日 星期三

Hide Dialog 既 語音識別


package tif.makeyourjarvis;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import android.util.Log;



public class MainActivity extends Activity implements OnClickListener
{
String resultstr;

   private TextView mText;
   private SpeechRecognizer sr;
   private static final String TAG = "MyStt3Activity";
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //Button speakButton = (Button) findViewById(R.id.btn_speak);    
            //mText = (TextView) findViewById(R.id.textView1);    
            //speakButton.setOnClickListener(this);
            sr = SpeechRecognizer.createSpeechRecognizer(this);      
            sr.setRecognitionListener(new listener());    
           
            speak();
   }

   private void speak() {
// TODO Auto-generated method stub
  Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);      
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
       intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
            sr.startListening(intent);
            Log.i("111111","11111111");

}

class listener implements RecognitionListener        
   {
            public void onReadyForSpeech(Bundle params)
            {
                     Log.d(TAG, "onReadyForSpeech");
            }
            public void onBeginningOfSpeech()
            {
                     Log.d(TAG, "onBeginningOfSpeech");
            }
            public void onRmsChanged(float rmsdB)
            {
                     Log.d(TAG, "onRmsChanged");
            }
            public void onBufferReceived(byte[] buffer)
            {
                     Log.d(TAG, "onBufferReceived");
            }
            public void onEndOfSpeech()
            {
                     Log.d(TAG, "onEndofSpeech");
            }
            public void onError(int error)
            {
                     Log.d(TAG,  "error " +  error);
                     Toast.makeText(getApplicationContext(), "error",Toast.LENGTH_SHORT).show();
            }
            public void onResults(Bundle results)                  
            {
                     String str = new String();
                     Log.d(TAG, "onResults " + results);
                     ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                     for (int i = 0; i < data.size(); i++)
                     {
                               Log.d(TAG, "result " + data.get(i));
                               str += (data.get(i)+", ");
                     }
                     Toast.makeText(getApplicationContext(), "results: "+str,Toast.LENGTH_SHORT).show();  
            }
            public void onPartialResults(Bundle partialResults)
            {
                     Log.d(TAG, "onPartialResults");
            }
            public void onEvent(int eventType, Bundle params)
            {
                     Log.d(TAG, "onEvent " + eventType);
            }
   }

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
}

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...