Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
コマンドプロンプトで UWP の音声認識APIを利用するサンプル。NuGet で UWPDesktop を追加してください。https://blogs.msdn.microsoft.com/lucian/2015/10/23/how-to-call-uwp-apis-from-a-desktop-vbc-app/
// コマンドプロンプトで UWP の音声認識APIを利用するサンプル
// NuGet で UWPDesktop を追加してください。
// https://blogs.msdn.microsoft.com/lucian/2015/10/23/how-to-call-uwp-apis-from-a-desktop-vbc-app/
using System;
using Windows.Media.SpeechRecognition;
namespace UWPCommandLine
{
class Program
{
//連続音声認識のためのオブジェクト
private static SpeechRecognizer contSpeechRecognizer;
static void Main(string[] args)
{
Initialize();
Console.ReadKey();
}
private static async void Initialize()
{
//初期化
contSpeechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
await contSpeechRecognizer.CompileConstraintsAsync();
//認識中の処理定義
contSpeechRecognizer.HypothesisGenerated += ContSpeechRecognizer_HypothesisGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
// 音声入力ないままタイムアウト(20秒位)した場合の処理
contSpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
//認識開始
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private static async void ContinuousRecognitionSession_Completed(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionCompletedEventArgs args)
{
Console.WriteLine("タイムアウトしました");
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private static void ContSpeechRecognizer_HypothesisGenerated(SpeechRecognizer sender, SpeechRecognitionHypothesisGeneratedEventArgs args)
{
//認識途中に画面表示
Console.Write("\r" + args.Hypothesis.Text);
}
private static void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
//認識完了後に画面に表示
Console.WriteLine("\r" + args.Result.Text+"");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment