Android Open Source - speech Speech2txt Demo Activity






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.spk2txtDemo;
//from   w w w .  j a va 2  s. c  om
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/*
 * This is a simplified version of Google's voice recognition code.
 * It skips all the languages and just uses the default language.
 */

public class Speech2txtDemoActivity extends Activity {
  
    private static final String TAG = "VoiceRecognition";

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
  
  TextView log;
  Button spk;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spk = (Button) findViewById(R.id.button1);
        spk.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        
         startVoiceRecognitionActivity();
      }
        });
        log = (TextView) findViewById(R.id.log);
        
    }
    
    /**
     * Fire an intent to start the speech recognition activity.
     */
    private void startVoiceRecognitionActivity() {
        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());

        // Display an hint to the user about what he should say.
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");

        // 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 how many results you want to receive. The results will be sorted
        // where the first result is the one with higher confidence.
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);

        Log.i(TAG,"Calling the Voice Intenet");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
    /**
     * Handle the results from the recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
          
       
          // 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 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

          //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));
          }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
    
  /*
   * 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