谷歌眼鏡GDK開發指南之語音輸入

原文鏈接:http://bbs.seacat.cn/thread-901-1-1.html


Glass可以讓你聲明語音命令,從ok glass 語音菜單中啓動你的Glassware。


        




你也可以調用語音識別的activity







已存在語音命令


當你在開發的時候,你可以使用任何你想要的語音指令。當你想要啓動Glassware並出現在MyGlass中,你必須得使用已存在的命令或提交新命令審覈。



  • listen to
  • take a note
  • post an update
  • show a compass
  • start a run
  • start a bike ride
  • find a recipe
  • record a recipe
  • check me in
  • start a stopwatch
  • start a timer
  • start a round of golf
  • translate this
  • learn a song
  • tune an instrument
  • play a game
  • start a workout
注意:需要準確的聲明這些語音命令(包括間隔符)到<trigger>元素中。







啓動Glassware


添加一個trigger到ok glass語音主菜單:


1、爲你的voice trigger聲明一個string變量的名稱到res/values/strings.xml。可選擇性的聲明一個語音提示,在啓動Glassware前顯示在語音識別activity中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="glass_voice_trigger">take a note</string>
    <string name="glass_voice_prompt">tell me what's on your mind</string>
</resources>

2、爲 voice trigger創建一個xml資源到 res/xml/<my_voice_trigger>.xml 
一個簡單的voice trigger直接啓動一個activity或一個service


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

3、註冊一個action爲 com.google.android.glass.action.VOICE_TRIGGER 的intent filter在manifest中。當檢測到用戶說出你的voice trigger時會啓動這activity或service。


<?xml version="1.0" encoding="utf-8"?>
<application ...>
    <activity | service ...>
        <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/my_voice_trigger" />
    </activity | service>
    // ...
</application>


4、爲activity或service設置一個android:icon屬性,用來顯示在ok,glass菜單中。


注意:圖標必須是白色且背景透明,50*50像素。


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


5、當啓動了一個activity,可以通過下面的代碼得到轉義的文字


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


當啓動了一個服務,可以在 onStartCommand() 的回調中得到intent extra:


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


設置約束


如果你需要使用相機、網絡、麥克風這些功能,就需要在res/xml/<my_voice_trigger>.xml中指定他們。如果功能無效,Glass就會禁用這個 voice trigger:


<trigger keyword="@string/glass_voice_trigger">
    <constraints
        camera="true"
        network="true" />
</trigger>


啓動語音識別activity




語音識別activity會等待用戶說話,並在說完後返回轉義的文本。啓動這個activity:


1、通過 ACTION_RECOGNIZE_SPEECH intent來調用startActivityForResult()方法。支持以下intent extras :

EXTRA_PROMPT
EXTRA_RESULTS_PENDINGINTENT
EXTRA_RESULTS_PENDINGINTENT_BUNDLE




2、重寫 onActivityResult() 回調方法,從 EXTRA_RESULTS intent extra中接收轉義文本,當用戶停止說話的時候,這個回調會被調用。


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);
}



發佈了24 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章