Skip to content

Commit

Permalink
for #7 now lets go eyes free
Browse files Browse the repository at this point in the history
Want to open your garage door on voice command, or turn on the coffee machine from your bed?

Lets use the Voice Recognition intent to have the andoid understand you... or at least tell you what it thought you said. This commit shows adding a couple of functions to our make it talk code, to make it listen and repeat.
  • Loading branch information
cesine committed Feb 26, 2013
1 parent 84d9085 commit 857230b
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Tutorial/AndroidManifest.xml
Expand Up @@ -36,6 +36,10 @@
<activity
android:name=".practice.MakeItTalk"
android:label="Text To Speech" >
</activity>
<activity
android:name=".practice.MakeItListenAndRepeat"
android:label="Speech Recognition" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Binary file added Tutorial/MakeItListenAndRepeat.apk
Binary file not shown.
11 changes: 8 additions & 3 deletions Tutorial/res/values-fr/strings.xml
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="my_first_words">Bonsoir! Moi, je peux parler aussi! M'entendez vous?</string>
</resources>

<string name="my_first_words">Bonsoir! Moi, je peux parler aussi! M\'entendez vous?</string>
<string name="im_listening">Allez-y, Je vous écoute!</string>
<string name="i_might_have_heard">Je crois que j\'ai entendu: </string>
<string name="or_maybe">Ou bien: </string>
<string name="there_were_others"> J\'ai un sens de l\'humour, non? Il y avait d\'autres options, mais je crois que la, c\'est assez.</string>

</resources>
12 changes: 8 additions & 4 deletions Tutorial/res/values/strings.xml
Expand Up @@ -18,15 +18,18 @@
-->

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

<!-- General -->
<skip />
<!-- Application name -->
<string name="application_name">UpAndRunningWithAndroid</string>
<!-- Title, name of the activity used to create a gesture -->
<string name="label_create_gesture">Create a gesture</string>

<string name="my_first_words">I can talk! After all, I have speakers.</string>

<string name="im_listening">Talk to me, I\'m listening...</string>
<string name="i_might_have_heard">I might have heard: </string>
<string name="or_maybe">Or maybe this: </string>
<string name="there_were_others">. I\'m pretty funny, ey? There were some other options, but I think I\'ll stop there. </string>

<!-- Buttons -->
<skip />
Expand All @@ -38,7 +41,7 @@
<string name="button_discard">Discard</string>
<!-- Label, button used to save a gesture newly created -->
<string name="button_done">Done</string>

<!-- Gestures -->
<skip />
<!-- Label, prompt asking the user to enter the name of the gesture -->
Expand Down Expand Up @@ -69,4 +72,5 @@
<string name="gestures_rename_label">Gesture name</string>
<!-- Message, displayed when the sdcard cannot be found, 1st parameter is the name of the file that stores the gestures. CHAR LIMIT=80 -->
<string name="gestures_error_loading">Could not load %s. Make sure you have storage available.</string>
</resources>

</resources>
@@ -0,0 +1,133 @@
package com.androidmontreal.gesturevoicecommander.practice;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;

import com.androidmontreal.gesturevoicecommander.R;

/**
*
* Building on what we saw in MakeItTalk, now lets make it Listen. Here is some
* super simple code that uses the VoiceRecognition Intent to recognize what the
* user says, and then uses Text To Speech to tell the user what it might have
* heard.
*
* @author cesine
*
*/
public class MakeItListenAndRepeat extends Activity implements OnInitListener {
private static final String TAG = "MakeItListen";
private static final int RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE = 341;
/** Talk to the user */
private TextToSpeech mTts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mTts = new TextToSpeech(this, this);

}

protected void promptTheUserToTalk() {
mTts.speak(getString(R.string.im_listening), TextToSpeech.QUEUE_ADD, null);
}

/**
* Fire an intent to start the voice 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,
getString(R.string.im_listening));
startActivityForResult(intent, RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE);
}

/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE
&& resultCode == RESULT_OK) {
/*
* Populate the wordsList with the String values the recognition engine
* thought it heard, and then Toast them to the user and say them out
* loud.
*/
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for (int iMightHaveHeardThis = 0; iMightHaveHeardThis < matches.size(); iMightHaveHeardThis++) {

/* Build a carrierPhrase if you want it to make some sense */
String carrierPhrase = getString(R.string.i_might_have_heard);
if (iMightHaveHeardThis > 0) {
carrierPhrase = getString(R.string.or_maybe);
}
carrierPhrase += " " + matches.get(iMightHaveHeardThis) + ".";

Toast.makeText(this, carrierPhrase, Toast.LENGTH_LONG).show();
mTts.speak(carrierPhrase, TextToSpeech.QUEUE_ADD, null);

/*
* Don't go on forever, it there are too many potential matches don't
* say them all
*/
if (iMightHaveHeardThis == 2 && matches.size() > 2) {
mTts.speak(getString(R.string.there_were_others),
TextToSpeech.QUEUE_ADD, null);
break;
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
protected void onDestroy() {
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}

@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e(TAG, "Language is not available.");
Toast.makeText(
this,
"The " + Locale.getDefault().getDisplayLanguage()
+ " TextToSpeech isn't installed, you can go into the "
+ "\nAndroid's settings in the "
+ "\nVoice Input and Output menu to turn it on. ",
Toast.LENGTH_LONG).show();
} else {
// everything is working.
promptTheUserToTalk();
startVoiceRecognitionActivity();
}
} else {
Toast.makeText(
this,
"Sorry, I can't talk to you because "
+ "I could not initialize TextToSpeech.", Toast.LENGTH_LONG)
.show();
}
}
}
@@ -1,18 +1,32 @@
package com.androidmontreal.gesturevoicecommander.practice;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;

import com.androidmontreal.gesturevoicecommander.R;

/**
*
* Building on what we saw in MakeItTalk, now lets make it Listen. Here is some
* super simple code that uses the VoiceRecognition Intent to recognize what the
* user says, and then uses Text To Speech to tell the user what it might have
* heard.
*
* @author cesine
*
*/
public class MakeItTalk extends Activity implements OnInitListener {
private static final String TAG = "MakeItTalk";
private static final String TAG = "MakeItListen";
private static final int RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE = 341;
/** Talk to the user */
private TextToSpeech mTts;

Expand All @@ -24,8 +38,60 @@ protected void onCreate(Bundle savedInstanceState) {

}

protected void sayFirstWords() {
mTts.speak(getString(R.string.my_first_words), TextToSpeech.QUEUE_ADD, null);
protected void promptTheUserToTalk() {
mTts.speak(getString(R.string.im_listening), TextToSpeech.QUEUE_ADD, null);
}

/**
* Fire an intent to start the voice 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,
getString(R.string.im_listening));
startActivityForResult(intent, RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE);
}

/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RETURN_FROM_VOICE_RECOGNITION_REQUEST_CODE
&& resultCode == RESULT_OK) {
/*
* Populate the wordsList with the String values the recognition engine
* thought it heard, and then Toast them to the user and say them out
* loud.
*/
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for (int iMightHaveHeardThis = 0; iMightHaveHeardThis < matches.size(); iMightHaveHeardThis++) {

/* Build a carrierPhrase if you want it to make some sense */
String carrierPhrase = getString(R.string.i_might_have_heard);
if (iMightHaveHeardThis > 0) {
carrierPhrase = getString(R.string.or_maybe);
}
carrierPhrase += " " + matches.get(iMightHaveHeardThis) + ".";

Toast.makeText(this, carrierPhrase, Toast.LENGTH_LONG).show();
mTts.speak(carrierPhrase, TextToSpeech.QUEUE_ADD, null);

/*
* Don't go on forever, it there are too many potential matches don't
* say them all
*/
if (iMightHaveHeardThis == 2 && matches.size() > 2) {
mTts.speak(getString(R.string.there_were_others),
TextToSpeech.QUEUE_ADD, null);
break;
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
Expand Down Expand Up @@ -53,12 +119,12 @@ public void onInit(int status) {
Toast.LENGTH_LONG).show();
} else {
// everything is working.
sayFirstWords();
promptTheUserToTalk();
startVoiceRecognitionActivity();
}
} else {
Toast.makeText(
this,

"Sorry, I can't talk to you because "
+ "I could not initialize TextToSpeech.", Toast.LENGTH_LONG)
.show();
Expand Down

0 comments on commit 857230b

Please sign in to comment.