Friday 4 April 2014

Capture Speech/Voice Input for Google Glass

This tutorial will walk you through the steps and code snippets for capturing user speech/voice input in your GDK based Glassware.
Step 1:
Create an Android project as shown below. If you haven’t tried installing any sample Glassware on your Google Glass, please visit this tutorial.
Create an Android Project
Create an Android Project
Step 2:
Create an Android Activity and edit the AndroidManifest.xml as shown below:
<activity
android:name=”com.example.capturespeechinput.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<action android:name=”com.google.android.glass.action.VOICE_TRIGGER” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
<meta-data
android:name=”com.google.android.glass.VoiceTrigger”
android:resource=”@xml/voice_trigger_start” />
</activity>
Step 3:
Now, create a folder called xml under res and create an xml file called voice_trigger_start.xml.
Paste the following content inside:
<?xml version=”1.0″ encoding=”utf-8″?>
<trigger keyword=”Capture Speech” >
         <input prompt=”Speak now!” />
</trigger>
Step 4:
Edit the activity_main.xml (Your activity’s layout page) to include a TextView with the id – ‘capturedSpeechToText’ as follows:
<TextView
android:id=”@+id/capturedSpeechToText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”" />
Step 5:
Edit your MainActivity Java Class to include the following code snippet to captured the spoken text in the onCreate() method:
ArrayList<String> voiceResults = getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
if (voiceResults != null && voiceResults.size() > 0) {
String spokenText = voiceResults.get(0);
TextView capturedSpeechToTextViewObj = ((TextView) findViewById(R.id.capturedSpeechToText));
capturedSpeechToTextViewObj.setText(spokenText);
}
Step 6:
Now, build and deploy the Android application on to your Google Glass and enjoy the output:
Capture Speech - Launch Application
Capture Speech – Launch Application
Voice Prompt - Speak Now
Voice Prompt – Speak Now
Voice Input for Google Glass
Voice Input for Google Glass
Voice captured in your GDK Application
Voice captured in your GDK Application

No comments:

Post a Comment