Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 35736

自分の決めたキーワードでアプリを起動する

$
0
0

#wpdev_jp #wpjp

さて、アプリケーションはOSの持っている音声認識機能を使って「起動 アプリ名」で起動させることができます。

しかし、アプリ側でキーワードを登録してやれば、そのキーワードを使って起動させることもできます。(起動 という必要はありません)ただ条件があって コマンド+オプション という構成でないといけません。コマンドだけではだめなんですね。

まずはそのための定義ファイルを作りますが、あらかじめSDKにサンプルがあるので簡単に追加できます。プロジェクトメニューの「新しい項目の追加」 を選んだら、ダイアログの中から音声コマンドの定義を選びます。

image

音声コマンドにはあらかじめ設定が書いてあります。実は音声でオプション(引数)も設定できるようになっており、そのためのサンプル設定も記載してあります。

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
  <CommandSet xml:lang="ja-jp">
    <CommandPrefix>Contoso Rodeo</CommandPrefix>
    <Example> play a new game </Example><Command Name="PlayGame">
      <Example> play a new game </Example>
      <ListenFor> [and] play [a] new game </ListenFor>
      <ListenFor> [and] start [a] new game </ListenFor>
      <Feedback> 新しいゲームを開始しています... </Feedback>
      <Navigate />
    </Command>

    <Command Name="PlayLevel">
      <Example> replay level two </Example>
      <ListenFor> replay level {number} </ListenFor>
      <Feedback> レベル {number} に移動しています... </Feedback>
      <Navigate />
    </Command>

    <Command Name="PlayUnknownLevel">
      <Example> replay level two </Example>
      <ListenFor> [and] replay level {*} </ListenFor>
      <Feedback> 不明なレベルです。レベルの選択に移動しています... </Feedback>
      <Navigate Target="LevelSelect.xaml" />
    </Command>

    <PhraseList Label="number">
      <Item> 1 </Item>
      <Item> 2 </Item>
      <Item> 3 </Item>
    </PhraseList>

  </CommandSet>
</VoiceCommands>

CommandSet がこの起動コマンドで、各言語ごとに用意することができます。その中のCommand がオプションのキーワードの定義です。「1日前の画像」みたいなオプション認識も可能ですが、それはまた今度。要は2つのキーわーで構成されるように、CommandSet と Command を作ってしまえばOK。

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
  <CommandSet xml:lang="ja-jp">
    <CommandPrefix>アムロ</CommandPrefix>
    <Example> アムロ行きまーす </Example>

    <Command Name ="no command">
      <Example>行きまーす</Example>
      <ListenFor>行きまーす</ListenFor>
      <Feedback>どうぞ</Feedback>
      <Navigate/>
    </Command>

  </CommandSet>
</VoiceCommands>

ただ、これはそのままではOSには登録されません。1度だけ登録作業が必要です。つまり1度は起動しないといけないのです。今回は App.Xaml.cs の Application_Launchingに記述しました。Async を追加を忘れないように。

 

private async void Application_Launching(object sender, LaunchingEventArgs e)
{
    await Windows.Phone.Speech.VoiceCommands.
        VoiceCommandService.
        InstallCommandSetsFromFileAsync(
        new Uri("ms-appx:///VoiceCommandDefinition1.xml")
        );
}

これでOK。あとはF5キーを押してエミュレーターで実行してみましょう。

  1. 実行したら戻るボタン(エミュレーターではF1)を押してアプリを終了します。
  2. ホームボタン(エミュレーターではF2)を長押しして音声認識機能を起動します。
  3. 「アムロ 行きまーす」でアプリが起動します。
  4. 認識されると 「どうぞ」 と何の感情もこもっていない声が出てアプリが起動します。

Command の ListenFor が起動オプションになりますが、ここを空白とか内容無しにするとそもそもコンパイルが通らないので、コマンド+オプションの形式だけ注意してください。


Viewing all articles
Browse latest Browse all 35736

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>