• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Speech to text app

ajith19

Lurker
Aug 22, 2010
2
0
I want to create speech to text app,to get started i got sample code from android developer site like this..
package com.example.android.apis.app;

package com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
* Sample code that invokes the speech recognition intent API.
*/
public class VoiceRecognition extends Activity implements OnClickListener {

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

private ListView mList;

/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Inflate our UI from its XML layout description.
setContentView(R.layout.voice_recognition);

// Get display items for later interaction
Button speakButton = (Button) findViewById(R.id.btn_speak);

mList = (ListView) findViewById(R.id.list);

// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else {
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
}

/**
* Handle the click on the start recognition button.
*/
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startVoiceRecognitionActivity();
}
}

/**
* Fire an intent to start the speech recognition activity.
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
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
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
}

super.onActivityResult(requestCode, resultCode, data);



And i have created,voice_recognition.xml in layout and made changes too...As given in the code,Araylist??should i create arraylist?/and what is simple_list_item_1??should it be created in the layout or is it present in the api??need help..:thinking:
 
1) Arraylist is an object. It has nothing to do with an XML file, well, directly anyways...

2) android.R.simple_list_item_1 is a pre-assembled layout that comes with the android SDK. Any resource preceded with android.R is an example resource that comes with the android SDK. Although you COULD just use this resource, if you plan to place it in the android market, I would highly recommend creating your own resource to use in place of it, else you will just be laughed at by other devs (no offense).
 
Upvote 0
The sample from the android site works totally fine. If you download the samples from the site, you would also get the lay out xml along with it. In this particular example, they have provided a button, on clicking this button, the voice recognition activity starts up and record the speech/voice commands. Then it shows the list of the words it recognized.
The xml -
HTML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="4dip"
        android:text="Speak Up" />
        
    <Button android:id="@+id/btn_speak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Speak" />
        
    <ListView android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

 </LinearLayout>
 
Upvote 0
I think you meant speech to text.
So basically, as mentioned earlier, basic example is available online on android site.

Android sdk provides Voice Recognition Activity, which we have to use. So one can code, to start this activity on some user action. You can also show a prompt to user mentioning things if you are expecting certain type of format (Example - Voice Dial in android phones. They mention there some commands/patterns they are expecting like "Call John" etc).

This activity can be configured in various ways as well. Refer here

The voice recognition activity, would try to recognize the voice input and return back list of stringsthat it recognized. Based on this result, we can if required confirm from user if this is what he/she meant and proceed further.

The code mentioned above, starts this voice recognition activity on button click.
First registering it here -
Code:
speakButton.setOnClickListener(this);
And then starting the activity here -
Code:
   public void onClick(View v) {
        if (v.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }

    /**
     * Fire an intent to start the speech recognition activity.
     */
    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

Then as the voice input is recognized, the result comes back -
Code:
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
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    matches));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

Its just that in this example, they are collecting all the recognized strings in a list and displaying in a ListView on the screen. You would be getting back the string/text and this can be used further in any way you want !!
Code:
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones