domingo, 28 de octubre de 2012

Reconocimiento de Voz

Si en nuestra aplicación es necesario un buscador web, de aplicaciones o similares, podemos usar la opción de búsqueda por voz. Este tipo de búsqueda se puede configurar para que sea interna al teléfono, por ejemplo aplicaciones, o bien, en la web.

En el siguiente ejemplo veremos que está configurado para web y que el número máximo de elementos que se van a mostrar en la lista son 10. Esta configuración se puede hacer dinámica: en la búsqueda por web o aplicaciones se puede realizar mediante la inserción de un RadioButton en el código. En el segundo caso podemos poner un contador, un spinner con el número de búsquedas limitadas...

A continuación les dejo el código:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.blogvoicerecognition"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".VoiceRecognitionActivity"
            android:label="@string/title_activity_voice_recognition" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_voice_recognition.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etTextHint"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Speech hint here"/>

    <Button
        android:id="@+id/btSpeak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="speak"
        android:padding="@dimen/padding_medium"
        android:text="Speak"
        tools:context=".VoiceRecognitionActivity" />

    <ListView
        android:id="@+id/lvTextMatches"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

VoiceRecognitionActivity.java
package com.example.blogvoicerecognition;

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

import android.app.Activity;
import android.app.SearchManager;
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.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class VoiceRecognitionActivity extends Activity {

private static final int VOICE_RECOGNITION_REQUEST_CODE = 0;
private static final int MATCHES = 10;

private EditText mEditText;
private ListView mListViewResults;
private Button mButtonSpeak;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_recognition);
mEditText = (EditText) findViewById(R.id.etTextHint);
mListViewResults = (ListView) findViewById(R.id.lvTextMatches);
mButtonSpeak = (Button) findViewById(R.id.btSpeak);
voiceRecognition();
}

public void voiceRecognition() {
// Check if voice recognition is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
mButtonSpeak.setEnabled(false);
mButtonSpeak.setText("Voice recognizer not present");
Toast.makeText(this, "Voice recognizer not present",
Toast.LENGTH_SHORT).show();
}
}

public void speak(View view) {
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, mEditText.getText()
.toString());

// Given an hint to the recognizer about what the user is going to say
// There are two form of language model available
// 1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases
// 2.LANGUAGE_MODEL_FREE_FORM : If not sure about the words or phrases
// and its domain.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

// 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, MATCHES);
// Start the Voice recognizer activity for the result.
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

// If Voice recognition is successful then it returns RESULT_OK
if (resultCode == RESULT_OK) {

ArrayList<String> textMatchList = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

if (!textMatchList.isEmpty()) {
// If first Match contains the 'search' word
// Then start web search.
if (textMatchList.get(0).contains("search")) {

String searchQuery = textMatchList.get(0);
searchQuery = searchQuery.replace("search", "");
Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
search.putExtra(SearchManager.QUERY, searchQuery);
startActivity(search);
} else {
// populate the Matches
mListViewResults.setAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1,
textMatchList));
}

}
// Result code for various error.
} else if (resultCode == RecognizerIntent.RESULT_AUDIO_ERROR) {
showToastMessage("Audio Error");
} else if (resultCode == RecognizerIntent.RESULT_CLIENT_ERROR) {
showToastMessage("Client Error");
} else if (resultCode == RecognizerIntent.RESULT_NETWORK_ERROR) {
showToastMessage("Network Error");
} else if (resultCode == RecognizerIntent.RESULT_NO_MATCH) {
showToastMessage("No Match");
} else if (resultCode == RecognizerIntent.RESULT_SERVER_ERROR) {
showToastMessage("Server Error");
}
super.onActivityResult(requestCode, resultCode, data);
}

/**
* Helper method to show the toast message
**/
void showToastMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

A continuación les dejo la imagen de la búsqueda de la palabra "Android":


It works!
Roger Sala,



1 comentario:

  1. Hola una pregunta que tipo de version de android es? Porque lo estoy haciendo en el 4.2 y me marca error en:mListViewResults = (ListView) findViewById(R.id.lvTextMatches);
    mButtonSpeak = (Button) findViewById(R.id.btSpeak);

    ResponderEliminar