Android Open Source - speech Main Fragment






From Project

Back to project page speech.

License

The source code is released under:

GNU General Public License

If you think the Android project speech listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.cs4730.spk2txtDemo2;
// w  ww. j  a  v  a  2  s . co  m
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/* 
 * This example shows the speech recognition without a dialog box.
 *   You should likely create your own, so people know when to speak and when it stops.
 *   This works for testing, but all the Log.i should likely be removed for real applications.
 *   
 */
public class MainFragment extends Fragment implements OnClickListener{
  private TextView log;
  private SpeechRecognizer sr;
  private static final String TAG = "spk2txtD2";
  
  public MainFragment() {
    // Required empty public constructor
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View myView = inflater.inflate(R.layout.fragment_main, container, false);
    Button speakButton = (Button) myView.findViewById(R.id.button1);    
    log = (TextView) myView.findViewById(R.id.log);     
    speakButton.setOnClickListener(this);
    //get the SpeechRecognizer and set a listener for it.
    sr = SpeechRecognizer.createSpeechRecognizer(getActivity());       
    sr.setRecognitionListener(new listener());   
    return myView;
  }

  @Override
    public void onDestroy() {
    sr.destroy();
    sr = null;
  }
  /*
   * The Recognitionlistener for the SpeechRecognizer.
   */
  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);
      logthis("error " + error);
    }
    public void onResults(Bundle results) {
      
      Log.d(TAG, "onResults " + results);
      // Fill the list view with the strings the recognizer thought it could have heard, there should be 5, based on the call
      ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            //display results.
      logthis("results: "+String.valueOf(matches.size())); 
      for (int i = 0; i < matches.size(); i++) {
        Log.d(TAG, "result " + matches.get(i));
        logthis("result " +i+":"+ matches.get(i));
      }

    }
    public void onPartialResults(Bundle partialResults)
    {
      Log.d(TAG, "onPartialResults");
    }
    public void onEvent(int eventType, Bundle params)
    {
      Log.d(TAG, "onEvent " + eventType);
    }
  }
  public void onClick(View v) {
    if (v.getId() == R.id.button1) {
      //get the recognize intent
      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      //Specify the calling package to identify your application
      intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
      //Given an hint to the recognizer about what the user is going to say
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      //specify the max number of results
      intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
      //User of SpeechRecognizer to "send" the intent.
      sr.startListening(intent);
      Log.i(TAG,"Intent sent");
    }
  }
  /*
   * simple method to add the log TextView.
   */
  public void logthis (String newinfo) {
    if (newinfo != "") {
      log.setText(log.getText() + "\n" + newinfo);
    }
  }
}




Java Source Code List

edu.cs4730.spk2txtDemo2.MainActivity.java
edu.cs4730.spk2txtDemo2.MainActivity.java
edu.cs4730.spk2txtDemo2.MainFragment.java
edu.cs4730.spk2txtDemo2.MainFragment.java
edu.cs4730.spk2txtDemo.MainActivity.java
edu.cs4730.spk2txtDemo.MainActivity.java
edu.cs4730.spk2txtDemo.MainFragment.java
edu.cs4730.spk2txtDemo.MainFragment.java
edu.cs4730.spk2txtDemo.Speech2txtDemoActivity.java
edu.cs4730.spk2txtDemo.Speech2txtDemoActivity.java
edu.cs4730.spk2txt.MainActivity.java
edu.cs4730.spk2txt.MainActivity.java
edu.cs4730.spk2txt.MainFragment.java
edu.cs4730.spk2txt.MainFragment.java
edu.cs4730.txt2spk.MainActivity.java
edu.cs4730.txt2spk.MainActivity.java
edu.cs4730.txt2spk.MainFragment.java
edu.cs4730.txt2spk.MainFragment.java