私はC#が初めてで、学ぶために、Webから例を見て試しています。
私はこの例を見ました:
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace MouseController
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Initialize();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
private void Initialize()
{
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
string line = "";
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.5f)
line += word.Text + " ";
}
string command = line.Trim();
switch (command)
{
case "left":
MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
break;
case "right":
MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
break;
case "up":
MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
break;
case "down":
MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
break;
}
txtOutput.Text += line;
txtOutput.Text += Environment.NewLine;
};
recognitionEngine.UnloadAllGrammars();
recognitionEngine.LoadGrammar(CreateGrammars());
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private Grammar CreateGrammars()
{
Choices commandChoices = new Choices("left", "right", "up", "down");
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(commandChoices);
return new Grammar(grammarBuilder);
}
private void MoveMouse(int x, int y)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(x, y);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
}
}
でも「上」「下」「左」「右」と言っても何も起こらない…
私もこれを試しました:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace SpeechRecognitionExample
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.8f)
txtOutput.Text += word.Text + " ";
}
txtOutput.Text += Environment.NewLine;
};
recognitionEngine.LoadGrammar(new DictationGrammar());
}
private void btnStart_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private void btnStop_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
}
}
そして、それは機能していません...
これらのコードの両方で、Visual Studio からエラーは発生しません。
なぜ機能しないのですか?
マイクを確認しましたが、動作しています。