You are on page 1of 4

VoiceInput

Voiceinputletsyoucreateatrulyhandsfreeinterface.Glassgivesyouthreewaystoutilizevoiceinput.

MainvoicecommandsstartGlasswarefromtheHomecard,contextualvoicecommandscanexecuteactionswithinanactivity,andthesystem'sspeechrecognitionactivityletsyoureceivefreeformvoice
inputfromusers.

Mainvoicecommands

ThesevoicecommandslaunchGlasswarefromtheHomecard(Clockcard).Whenyoudeclareamainvoicecommand,Glassautomaticallycreatesatouchmenuitemasafallbackifusersdecidetostart
yourGlasswarewithtappingontheHomecard.

Toaddavoicecommandtotheokglassvoicemainmenu:

1.CreateanXMLresourceforthevoicecommandinres/xml/<my_voice_trigger>.xmlthatusesoneoftheexistingvoicecommandsdefinedinVoiceTriggers.Command
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/VoiceTriggers.Command).Forexample,here'showtouse"Startarun".

star Note:Youcanuseunlistedvoicecommands(#unlisted_commands)fordevelopmentpurposes.

<?xmlversion="1.0"encoding="utf8"?>
<triggercommand="START_A_RUN"/>

Tocreateavoicecommandthatpromptstheusertospeakanadditionalphrasebeforestartingyouractivityorservice,includeaninputelementaswell.Forexample,youmightwanttodothisifyou
areusing"Postanupdate".

<?xmlversion="1.0"encoding="utf8"?>
<triggercommand="POST_AN_UPDATE">
<inputprompt="@string/glass_voice_prompt"/>
</trigger>

2.Registeranintentfilterusingthecom.google.android.glass.action.VOICE_TRIGGERactioninyourAndroidmanifest.Theintentfilterstartsyouractivityorserviceifitdetectsusersspeakingyour
voicecommand.

<?xmlversion="1.0"encoding="utf8"?>
<application...>
<activity|service...>
<intentfilter>
<actionandroid:name=
"com.google.android.glass.action.VOICE_TRIGGER"/>
</intentfilter>
<metadataandroid:name="com.google.android.glass.VoiceTrigger"
android:resource="@xml/my_voice_trigger"/>
</activity|service>
//...
</application>

3.Declareanandroid:iconattributeforyouractivityorservice.ThisallowsGlasstodisplayaniconforyourGlasswareintheok,glasstouchmenu.

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

4.Ifyourvoicecommandusesavoicepromptandstartsanactivity,obtainanytranscribedtextwiththefollowingcode(suchasinonResume()
(http://developer.android.com/reference/android/app/Activity.html#onResume())):

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

Ifthevoicecommandstartsaservice,theintentextraisavailableintheonStartCommand()(http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent,int,int))
callback:

@Override
publicintonStartCommand(Intentintent,intflags,intstartId){
ArrayList<String>voiceResults=intent.getExtras()
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
//...
}

Settingconstraints

IfyouneedoneorallofthefollowingfeaturestostartyourGlassware,specifythemintheres/xml/<my_voice_trigger>.xmlresource.Ifthefeaturesarenotavailable,Glassdisablesthevoicecommand:

camera

network

microphone

<triggercommand="POST_AN_UPDATE">
<constraints
camera="true"
network="true"/>
</trigger>
Contextualvoicecommands

Contextualvoicecommandsallowuserstocarryoutactionsfromactivities.YoubuildcontextualvoicecommandswiththestandardAndroidmenuAPIsbutuserscaninvokethemenuitemswithvoice
commandsinsteadoftouch.

Toenablecontextualvoicecommandsforaparticularactivity:

1.CallgetWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/view/WindowUtils#FEATURE_VOICE_COMMANDS)
)inthedesiredactivitytoenablecontextualvoicecommands.Withthisfeatureenabled,the"okglass"menuappearsinthefooterareaofthescreenwheneverthisactivityreceivesfocus.

2.OverrideonCreatePanelMenu()(http://developer.android.com/reference/android/app/Activity.html#onCreatePanelMenu(int,android.view.Menu))andhandlethecasewhereWindowUtils.FEATURE_VOICE_COMMANDS
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/view/WindowUtils#FEATURE_VOICE_COMMANDS)isenabled.Ifenabled,thisiswhereyoudoonetimemenusetup,like
inflatingamenuresourceorcallingtheMenu.add()(http://developer.android.com/reference/android/view/Menu.html#add(java.lang.CharSequence))methodstocreateyourvoicemenusystem.
3.OverrideonMenuItemSelected()(http://developer.android.com/reference/android/view/Window.Callback.html#onMenuItemSelected(int,android.view.MenuItem))tohandlethevoicecommandswhenusersspeak
them.Whenusersaredoneselectingamenuitem,the"ok,glass"voicecommandautomaticallyreappearsinthefootersectionofthescreen,readytoacceptanewvoicecommand,aslongasthe
activityremainsinfocus.
Thefollowingcodeenablescontextualvoicecommands,inflatesamenuresourcewhenappropriate,andhandlesvoicecommandswhentheyarespoken:

publicclassContextualMenuActivityextendsActivity{

@Override
protectedvoidonCreate(Bundlebundle){
super.onCreate(bundle);

//Requestsavoicemenuonthisactivity.Asforanyother
//windowfeature,besuretorequestthisbefore
//setContentView()iscalled
getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
setContentView(R.layout.activity_main);
}

@Override
publicbooleanonCreatePanelMenu(intfeatureId,Menumenu){
if(featureId==WindowUtils.FEATURE_VOICE_COMMANDS){
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
//Passthroughtosupertosetuptouchmenu.
returnsuper.onCreatePanelMenu(featureId,menu);
}

@Override
publicbooleanonCreateOptionsMenu(Menumenu){
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}

@Override
publicbooleanonMenuItemSelected(intfeatureId,MenuItemitem){
if(featureId==WindowUtils.FEATURE_VOICE_COMMANDS){
switch(item.getItemId()){
caseR.id.dogs_menu_item:
//handletopleveldogsmenuitem
break;
caseR.id.cats_menu_item:
//handletoplevelcatsmenuitem
break;
caseR.id.lab_menu_item:
//handlesecondlevellabradormenuitem
break;
caseR.id.golden_menu_item:
//handlesecondlevelgoldenmenuitem
break;
caseR.id.calico_menu_item:
//handlesecondlevelcalicomenuitem
break;
caseR.id.cheshire_menu_item:
//handlesecondlevelcheshiremenuitem
break;
default:
returntrue;
}
returntrue;
}
//Goodpracticetopassthroughtosuperifnothandled
returnsuper.onMenuItemSelected(featureId,item);
}
}

Here'sanexampleofthemenuresourceusedbythepreviousactivity.Noticehowyoucancreatenestedmenuitemsforahierarchicalvoicemenusystem.Inthefollowingexample,thefirstmenu
itemcanbeaccessedas:okglass,Showmedogs,Labrador.

<menuxmlns:android="http://schemas.android.com/apk/res/android">
<!UsetheconstantsdefinedintheContextualMenus.Commandenum>
<item
android:id="@+id/dogs_menu_item"
android:title="@string/show_me_dogs">
<menu>
<item
android:id="@+id/lab_menu_item"
android:title="@string/labrador"/>
<item
android:id="@+id/golden_menu_item"
android:title="@string/golden"/>
</menu>
</item>
<item
android:id="@+id/cats_menu_item"
android:title="@string/show_me_cats">
<menu>
<item
android:id="@+id/cheshire_menu_item"
android:title="@string/cheshire"/>
<item
android:id="@+id/calico_menu_item"
android:title="@string/calico"/>
</menu>
</item>
</menu>

star Note:Themenutitlesinthepreviousmenuresourceusecustomstrings,whichyoucandoifyouspecifythedevelopmentpermission.(#unlisted_commands)InyourGlassware,usethevaluesinthe
ContextualMenus.Command(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/ContextualMenus.Command)enum.Forexample,tousethePLAY_MUSIC
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/ContextualMenus.Command#PLAY_MUSIC)andPAUSE_MUSIC
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/ContextualMenus.Command#PAUSE_MUSIC)commands,youcancreatethefollowingmenuresource:

<menuxmlns:android="http://schemas.android.com/apk/res/android">
<!UsetheconstantsdefinedintheContextualMenus.Commandenum>
<item
android:id="@+id/play_menu_item"
android:title="PLAY_MUSIC"/>
<item
android:id="@+id/pause_menu_item"
android:title="PAUSE_MUSIC"/>
</menu>

4.(Optional)OverrideonPreparePanel()(http://developer.android.com/reference/android/view/Window.Callback.html#onPreparePanel(int,android.view.View,android.view.Menu)),checkingwhetherornot
WindowUtils.FEATURE_VOICE_COMMANDS(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/view/WindowUtils#FEATURE_VOICE_COMMANDS)isenabled.Ifenabled,thisis
whereyoucandootherlogictosetupthemenusystem,suchasaddingandremovingcertainmenuitemsbasedonsomecriteria.Youcanalsotogglecontextualvoicemenuson(returntrue)and
off(returnfalse)basedonsomecriteria.Forexample:

privatebooleanmVoiceMenuEnabled;
...
@Override
publicbooleanonPreparePanel(intfeatureId,Viewview,Menumenu){
if(featureId==WindowUtils.FEATURE_VOICE_COMMANDS){
//togglethisbooleanonandoffbasedonsomecriteria
returnmVoiceMenuEnabled;
}
//Goodpracticetocallthroughtosuperforothercases
returnsuper.onPreparePanel(featureId,view,menu);
}

Supportingvoiceandtouchmenussimultaneously

BecausecontextualvoicecommandsusetheexistingAndroidmenuAPIs,youcanreusealotofthecodeandresourcesyoualreadyhavefortouchmenusandsimultaneouslysupportbothtypesof
menus.

AllyouneedtodoischeckfortheWindow.FEATURE_OPTIONS_PANELfeatureinadditiontotheWindowUtils.FEATURE_VOICE_COMMANDS
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/view/WindowUtils#FEATURE_VOICE_COMMANDS)featurethatyoualreadyarecheckingforinafewmethodsandthenaddin
logictoopenthetouchmenuonsomeuseraction,suchasatap.

Forexample,youcanchangethepreviousactivityexampletoaddsupportfortouchmenuslikethis(changesarecommented):

//1.CheckforWindow.FEATURE_OPTIONS_PANEL
//toinflatethesamemenuresourcefortouchmenus.
@Override
publicbooleanonCreatePanelMenu(intfeatureId,Menumenu){
if(featureId==WindowUtils.FEATURE_VOICE_COMMANDS||
featureId==Window.FEATURE_OPTIONS_PANEL){
...
}

//2.CheckforWindow.FEATURE_OPTIONS_PANEL
//tohandletouchmenuitemselections.
@Override
publicbooleanonMenuItemSelected(intfeatureId,MenuItemitem){
if(featureId==WindowUtils.FEATURE_VOICE_COMMANDS||
featureId==Window.FEATURE_OPTIONS_PANEL){
...
}

Withthesechanges,youcaneithertaporsayokglasstodisplayyourmenu.

Usingunlistedvoicecommandsfordevelopment

WhenyouwanttodistributeyourGlassware,youmustusetheapprovedmainvoicecommandsinVoiceTriggers.Command
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/VoiceTriggers.Command)andapprovedcontextualvoicecommandsinContextualMenus.Command
(https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/ContextualMenus.Command).

IfyouwanttousevoicecommandsthatarenotavailableintheGDK,youcanrequestanAndroidpermissioninyourAndroidManifest.xmlfile:

<usespermission
android:name="com.google.android.glass.permission.DEVELOPMENT"/>

Note:Thisfeatureisfordevelopmentpurposesonly.

Usingunlistedmainvoicecommands

1.Declareastringvalueinres/values/strings.xmlthatdefinesthenameofyourvoicetrigger.OptionallydeclareavoiceprompttodisplaythespeechrecognitionGlasswarebeforestartingyour
Glassware.

<?xmlversion="1.0"encoding="utf8"?>
<resources>
<stringname="glass_voice_trigger">readmeastory</string>
<stringname="glass_voice_prompt">whatstory?</string>
</resources>

2.CreateanXMLresourceforthevoicetriggerinres/xml/<my_voice_trigger>.xml.Forunlistedvoicecommands,youshouldusethekeywordattributeinsteadofthecommandattributeusedfor
approvedvoicecommands.Thekeywordattributeshouldbeareferencetothestringresourcedefiningthevoicecommand.Forasimplevoicetriggerthatstartsanactivityorserviceimmediately,
simplyspecifythetriggerelement:

<?xmlversion="1.0"encoding="utf8"?>
<triggerkeyword="@string/glass_voice_trigger"/>

Tocreateavoicetriggerthatpromptstheusertospeakanadditionalphrasebeforestartingyouractivityorservice,includeaninputelementaswell:

<?xmlversion="1.0"encoding="utf8"?>
<triggerkeyword="@string/glass_voice_trigger">
<inputprompt="@string/glass_voice_prompt"/>
</trigger>

Usingunlistedcontextualvoicecommands

Whencreatingmenuitems,useanytextforthetitleofthemenuitem.\Forexample:

<menuxmlns:android="http://schemas.android.com/apk/res/android">
<!UsetheconstantsdefinedintheContextualMenus.Commandenum>
<item
android:id="@+id/pizza_menu_item"
android:title="@string/find_pizza"/>
</menu>

Startingspeechrecognition

ThespeechrecognitionGlasswarewaitsforuserstospeakandreturnsthetranscribedtextaftertheyaredone.Tostarttheactivity:

1.CallstartActivityForResult()(http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,int))withtheACTION_RECOGNIZE_SPEECH
(http://developer.android.com/reference/android/speech/RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH)intent.Thefollowingintentextrasaresupportedwhenstartingtheactivity:

EXTRA_PROMPT(http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_PROMPT)

EXTRA_RESULTS_PENDINGINTENT(http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_RESULTS_PENDINGINTENT)

EXTRA_RESULTS_PENDINGINTENT_BUNDLE(http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_RESULTS_PENDINGINTENT_BUNDLE)

2.OverridetheonActivityResult()(http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int,int,android.content.Intent))callbacktoreceivethetranscribedtextfromthe
EXTRA_RESULTS(http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_RESULTS)intentextra.Thiscallbackiscalledwhenusersfinishspeaking.

privatestaticfinalintSPEECH_REQUEST=0;

privatevoiddisplaySpeechRecognizer(){
Intentintent=newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(intent,SPEECH_REQUEST);
}

@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,
Intentdata){
if(requestCode==SPEECH_REQUEST&&resultCode==RESULT_OK){
List<String>results=data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
StringspokenText=results.get(0);
//DosomethingwithspokenText.
}
super.onActivityResult(requestCode,resultCode,data);
}

Exceptasotherwisenoted,thecontentofthispageislicensedundertheCreativeCommonsAttribution3.0License(http://creativecommons.org/licenses/by/3.0/),andcodesamplesarelicensedundertheApache2.0License
(http://www.apache.org/licenses/LICENSE2.0).Fordetails,seeourSitePolicies (https://developers.google.com/terms/sitepolicies).JavaisaregisteredtrademarkofOracleand/oritsaffiliates.

TerakhirdiperbaruiMei28,2015.

Videos StackOverflow Google+Community


Watchvideosonhowusers CheckoutourStackOverflow FollowusonGoogle+tostay
interactwithGlassintheir tagtofindandaskquestions uptodateonGlassandjoin
daytodaylives. aboutGlassdevelopment thediscussion!

You might also like