###前提・実現したいこと
現在、音声認識を利用したデスクトップアプリケーションを作成しています。
作成している内容については声でiPhoneを操作できる「Siri」のようにデスクトップ上でも声を使い、アプリを立ち上げる(終了させる)などの操作が出来るというものです。
基礎となるプログラムは、あらかた完成したのですがこのアプリを操作するためのUIの作成に苦戦しています。
完成させたい操作手順は以下の通りです。
1.アプリケーションのアイコンを指定のボックスにD&Dしてパスを取得する。
2.テキストボックスにアプリケーションを起動する際に発する言葉を入力する。
3.OKボタンを押すとアプリケーションのパスと入力された言葉を辞書ファイルに登録する。
4.マイクに向かって登録した言葉を発すると指定のアプリケーションを起動する。
1項目のD&Dして指定のアプリケーションのパスを取得することは、できましたがその取得したパスと起動するときに発する言葉を「Form1.cs」の「アプリの追加」項目への追加。「grammar.xml」のファイルには、起動するときに発する言葉のみを追加したいのですがその追加方法がわかりません。
目指したい完成形としては、取得したパスと起動ワードをテキストボックスに入力し、「登録」ボタンを押すと追加される。追加されたパスとワードが下のリストに追加され、そのリストからパスや登録ワードの変更が出来るというものです。
このやり方をお教えてほしいです。よろしくお願いします。
下の画像は現在作成途中のUIになります。
また、何か追加した方が良いと思う機能がありましたら意見としてコメントいただけると幸いです。
###Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Speech.Recognition; using System.Xml; using System.Diagnostics; using System.Speech; namespace xxx { public partial class Form1 : Form { public Form1() { InitializeComponent(); { SpeechRecognitionEngine engine = new SpeechRecognitionEngine(); engine.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e) { int hour = DateTime.Now.Hour; //現在時刻(hour)を答える string min = DateTime.Now.Minute.ToString("d"); //現在時刻(min)を答える { if (e.Result.Confidence > 0.4f) //音声の聞き取り精度(0.1~1.0f) switch (e.Result.Text) { //時間 case "今何時"://ここに命令 if (hour < 13) { //am System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\am.wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\" + hour + ".wav"); System.Threading.Thread.Sleep(500); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\時.wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\" + min + ".wav"); System.Threading.Thread.Sleep(1200); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\分.wav"); } else { //pm System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\" + hour + ".wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\" + hour + ".wav"); System.Threading.Thread.Sleep(500); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\時.wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\" + min + ".wav"); System.Threading.Thread.Sleep(1200); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\分.wav"); } break; //アプリの追加 case "ワード"://ここに命令 System.Diagnostics.Process.Start(@"C:\xxx..."); break; case "エクセル"://ここに命令 System.Diagnostics.Process.Start(@"C:\xxx..."); break; case "パワポ"://ここに命令 case "パワーポイント": System.Diagnostics.Process.Start(@"C:\xxx..."); break; case "〇〇終了"://ここに命令 case "〇〇閉じる": System.Diagnostics.Process.Start(@"C:\xxx..."); Application.Exit(); //このアプリを終了する break; } } }; Grammar g = new Grammar(@"C:\Users\xxx\Documents\Visual Studio 2015\Projects\xxx\grammar.xml", "command"); engine.LoadGrammar(g); engine.LoadGrammar(new DictationGrammar()); engine.SetInputToDefaultAudioDevice(); engine.RecognizeAsync(RecognizeMode.Multiple); } } } } private void Form1_Load(object sender, EventArgs e) { } //ユーザーインターフェース private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.AllowDrop = true; this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(456, 19); this.textBox1.TabIndex = 0; this.textBox1.Text = "登録したいアプリをD&Dしてください"; this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); // // textBox2 // this.textBox2.Location = new System.Drawing.Point(12, 37); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(456, 19); this.textBox2.TabIndex = 1; this.textBox2.Text = "登録ワードを入力してください"; // // button1 // this.button1.Location = new System.Drawing.Point(393, 62); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 2; this.button1.Text = "クリア"; this.button1.UseVisualStyleBackColor = true; // // button2 // this.button2.Location = new System.Drawing.Point(312, 62); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 3; this.button2.Text = "登録"; this.button2.UseVisualStyleBackColor = true; // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(12, 95); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(456, 88); this.listBox1.TabIndex = 4; // // Form1 // this.ClientSize = new System.Drawing.Size(618, 195); this.Controls.Add(this.listBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Load += new System.EventHandler(this.Form1_Load_1); this.ResumeLayout(false); this.PerformLayout(); } //TextBoxのD&D private void TextBox_DragEnter(object sender, DragEventArgs e) { //ファイルがドラッグされている場合、カーソルを変更する if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Copy; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { //ドロップされたファイルの一覧を取得 string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false); if (fileName.Length <= 0) { return; } // ドロップ先がTextBoxであるかチェック TextBox txtTarget = sender as TextBox; if (txtTarget == null) { return; } //TextBoxの内容をファイル名に変更 txtTarget.Text = fileName[0]; } private void Form1_Load_1(object sender, EventArgs e) { } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }
###grammar.xml
<?xml version="1.0" encoding="UTF-8"?> <grammar xml:lang="ja-JP" root="command" tag-format="semantics/1.0" xmlns="http://www.w3.org/2001/06/grammar" version="1.0"> <rule scope="public" id="command"> <one-of> <item>今何時</item> <item>ワード</item> <item>エクセル</item> <item>パワポ</item> <item>パワーポイント</item> </one-of> </rule> </grammar>
###補足情報(言語/FW/ツール等のバージョンなど)
開発環境:visualstudio2015
開発言語:C#
OS:Windows8.1
回答1件
あなたの回答
tips
プレビュー