Friday, January 31, 2014

Customise Voice command for apps in Google Glass?



To enable customize voice command for apps in google glass, follow following steps -
1. Inside the manifest file add these tags under the service which you wanted to trigger on your voice command.


<intent-filter>    
      <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data    
    android:name="com.google.android.glass.VoiceTrigger"         
    android:resource="@xml/voice_trigger_start" />

2. And you have to create a folder called xml inside res and add a xml file named as

    voice_trigger_start.xml.


3. inside voice_trigger_start.xml file add following line as resources -

<?xml version="1.0" encoding="utf-8"?>
     <trigger keyword="@string/its_me_rakesh" />

4. Open the values folder inside the res folder and edit strings.xml, so it will look like this-

<resources>    
    <string name="app_name">custome_voice</string>
    <string name="its_me_rakesh">Hello Rakesh</string>    

</resources>


5. Declare an android:icon attribute for your activity or service. This allows Glass to display an icon for your Glassware in the ok, glass touch menu


<activity |service
 
android:icon="@drawable/my_icon" ...>
  ...
</activity | service>



6. If your voice trigger used a voice prompt and starts an activity, obtain any transcribed text with the following code (such as in onResume()):

ArrayList<String> voiceResults = getIntent().getExtras()
       
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);



7. If the voice trigger starts a service, the intent extra is available in the onStartCommand() callback:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   
ArrayList<String> voiceResults = intent.getExtras()
           
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
   
// ...
}



8. Starting the speech recognition activity -

The speech recognition activity waits for users to speak and returns the trascribed text after they are done. To start the activity:
  1. Call startActivityForResult() with the ACTION_RECOGNIZE_SPEECH intent. The following intent extras are supported when starting the activity:
    • EXTRA_PROMPT
    • EXTRA_RESULTS_PENDINGINTENT
    • EXTRA_RESULTS_PENDINGINTENT_BUNDLE
  2. Override the onActivityResult() callback to receive the transcribed text from the EXTRA_RESULTS intent extra. This callback is called when users finish speaking.

private static final int SPEECH_REQUEST = 0;
private void displaySpeechRecognizer() {
   
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    startActivityForResult
(intent, SPEECH_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
       
Intent data) {
   
if (requestCode == SPEECH_REQUEST && resultCode == RESULT_OK) {
       
List<String> results = data.getStringArrayListExtra(
               
RecognizerIntent.EXTRA_RESULTS);
       
String spokenText = results.get(0);
       
// Do something with spokenText.
   
}
   
super.onActivityResult(requestCode, resultCode, data);
}

 
 

Existing voice commands

When you are developing, you can use any voice trigger that you like. When you want to launch Glassware and have it appear in MyGlass, you must use one of these existing commands or submit a new command for approval
  1. listen to
  2. take a note
  3. post an update
  4. show a compass
  5. start a run
  6. start a bike ride
  7. find a recipe
  8. record a recipe
  9. check me in
  10. start a stopwatch
  11. start a timer
  12. start a round of golf
  13. translate this
  14. learn a song
  15. tune an instrument
  16. play a game
  17. start a workout
 
 
source - developer.google.com/glass