forked from cesine/UpAndRunningWithAndroid
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
224 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
Tutorial/src/com/androidmontreal/gesturevoicecommander/practice/MakeItListenAndRepeat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters