Quantcast
Channel: kenji matsuokaのニッキ
Viewing all articles
Browse latest Browse all 342

音声入力の機能を追加する

$
0
0
ウェアラブル向けアプリを作る 目次
原文

音声アクションはウェアラブル経験の重要な部分です。それによりユーザーは手を使わずに素早くアクション出来ます。Wearは2種類の音声アクションを提供します。

-システムが提供する
これらの音声アクションはタスクにもとづき、Wearプラットフォームに組み込まれています。音声アクションを喋った時にActivityを起動させたい場合にはそれをフィルターします。
例:「ノートを取る」や「アラームをセットする」などがあります。

-アプリが提供する
これらの音声アクションはアプリに基づき、ランチャーアイコンのように宣言します。ユーザーはそのアクションを使うために「開始」(※訳注現在のバージョンでは日本語設定時に正しく動作しません)としゃべり、起動するActivityを指定します。

システムが提供する音声アクションを宣言する

Android Wearプラットフォームは「ノートを取る」や「アラームをセットする」などのユーザーアクションに基づいたいくつかのvoice intentを提供します。それによりユーザーはやりたいことを喋ればシステムは最も適切なActivityを開始します。

ユーザーが音声アクションを喋った時に、あなたのアプリはActivityを開始するためにIntentをフィルターできます。もしサービスをバックグラウンドで開始したいときは、Acitivityを仮想的なキューとして表示し、Activity内でサービスを開始します。仮想的なキューを取り除くにはfinish()を呼びます。

例えば「ノートを取る」コマンドのIntent filterを宣言し、MyNoteActivityという名前のActivityを起動するには以下のようにします。

<activity android:name="MyNoteActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="com.google.android.voicesearch.SELF_NOTE" />
</intent-filter>
</activity>


WearがサポートしているVoice intentsは以下のとおりです。
名前フレーズの例intent
車/タクシーを呼ぶ"OK Google, get me a taxi"

"OK Google, call me a car"
Action
com.google.android.gms.actions.RESERVE_TAXI_RESERVATION
ノートを取る"OK Google, take a note"

"OK Google, note to self"
Action
android.intent.action.SEND
Category
com.google.android.voicesearch.SELF_NOTE
Extras
android.content.Intent.EXTRA_TEXT - a string with note body
アラームをセットする"OK Google, set an alarm for 8 AM"

"OK Google, wake me up at 6 tomorrow"
Action
android.intent.action.SET_ALARM
Extras
android.provider.AlarmClock.EXTRA_HOUR - an integer with the hour of the alarm.
android.provider.AlarmClock.EXTRA_MINUTES - an integer with the minute of the alarm
(these 2 extras are optional, either none or both are provided)
タイマーをセットする"Ok Google, set a timer for 10 minutes"Action
android.intent.action.SET_TIMER
Extras
android.provider.AlarmClock.EXTRA_LENGTH - an integer in the range of 1 to 86400 (number of seconds in 24 hours) representing the length of the timer
自転車に乗る・降りる"OK Google, start cycling"

"OK Google, start my bike ride"

"OK Google, stop cycling"
Action
vnd.google.fitness.TRACK
Mime Type
vnd.google.fitness.activity/biking
Extras
actionStatus - a string with the value ActiveActionStatus when starting and CompletedActionStatus when stopping.
走り始める・止まる"OK Google, track my run"

"OK Google, start running"

"OK Google, stop running"
Action
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/running
Extras
actionStatus - a string with the value ActiveActionStatus when starting and CompletedActionStatus when stopping
歩き始める・止まる"OK Google, start a workout"

"OK Google, track my workout"

"OK Google, stop workout"
Action
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/other
Extras
actionStatus - a string with the value ActiveActionStatus when starting and CompletedActionStatus when stopping
心拍数を表示する"OK Google, what’s my heart rate?"

"OK Google, what’s my bpm?"
Action
vnd.google.fitness.VIEW
Mime Type
vnd.google.fitness.data_type/com.google.heart_rate.bpm
万歩計を表示する"OK Google, how many steps have I taken?"

"OK Google, what’s my step count?"
Action
vnd.google.fitness.VIEW
Mime Type
vnd.google.fitness.data_type/com.google.step_count.cumulative
プラットフォームのIntentを登録し、Extras情報にアクセスするにはCommon intentsを呼びます。

アプリが提供する音声アクションを宣言する

もし、適切なプラットフォームvoice intentがない場合、「Start MyActivityName」音声アクションで直接アプリを開始できます。
開始アクションを登録するには携帯端末のランチャーアイコンを登録するのと同じ情報です。ランチャーのアプリアイコンの代わりに音声アクションが必要です。

開始といったあとのテキストを指定するためlabel属性を開始するActivityに指定します。例えば、このIntent Filterは「開始 MyRunningApp」という音声アクションでStartRunActivityが開始されます。

<application>
<activity android:name="StartRunActivity" android:label="MyRunningApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

自由な形式の音声入力

音声アクションによるActivityの起動に加えて、システムビルドインの音声入力Activityを呼んでユーザーに音声入力させることも出来ます。これは検索やメッセージを送信するためにユーザーに入力させるのに便利です。

アプリ内でACTION_RECOGNIZE_SPEECHアクションを使いstartActivityForResult()を呼びます。これにより音声入力Activityを開始し、onActivityResult()で結果を受け取ることが出来ます。


private static final int SPEECH_REQUEST_CODE = 0;

// 音声入力activityを起動できるActivityを作成する
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// テキストを入力させるActivityを開始する。
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// 音声入力から戻ってきたらこのコールバックが発行されます。
// ここでIntentとテキストの入力内容が入ったIntentのextractを処理します。
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
// 喋った内容について処理する。
}
super.onActivityResult(requestCode, resultCode, data);
}



Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content License.

Viewing all articles
Browse latest Browse all 342

Trending Articles