此 C# winform 中未生成事件,它使用 azure 服务将语音转换为文本
Events not generated in this C# winform which uses azure services for converting speech to text
我有这个 c# winform,它使用 azure speech to text 将语音转换为文本。此 winform 有一个语音 on/off 复选框并执行连续语音识别,直到我关闭 window 。问题是,当我选中复选框时,只生成会话启动事件,没有其他任何事情发生,没有生成其他事件,如识别、识别、取消。你知道出了什么问题吗?
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 Microsoft.CognitiveServices.Speech;
namespace CsharpSTTform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) await SpeechContinuousRecognitionAsync();
}
public async Task SpeechContinuousRecognitionAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("api key", "westus");
// Creates a speech recognizer from microphone.
using (var recognizer = new SpeechRecognizer(config))
{
// Subscribes to events.
recognizer.Recognizing += (s, e) =>
{
Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
};
recognizer.Recognized += (s, e) =>
{
var result = e.Result;
Console.WriteLine($"Reason: {result.Reason.ToString()}");
if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"Final result: Text: {result.Text}.");
}
};
recognizer.Canceled += (s, e) =>
{
Console.WriteLine($"\n Recognition Canceled. Reason: {e.Reason.ToString()}, CanceledReason: {e.Reason}");
};
recognizer.SessionStarted += (s, e) =>
{
Console.WriteLine("\n Session started event.");
};
recognizer.SessionStopped += (s, e) =>
{
Console.WriteLine("\n Session stopped event.");
};
// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
}
}
}
}
当我选中复选框时,我得到的初始输出是:
Session started event.
The thread 0x3274 has exited with code 0 (0x0).
一段时间后我得到了这个输出:
Session started event.
The thread 0x3274 has exited with code 0 (0x0).
The thread 0x5880 has exited with code 0 (0x0).
The thread 0x9e8 has exited with code 0 (0x0).
您的 recognizer
变量是方法 SpeechContinuousRecognitionAsync
的 局部变量 ,它会在 using
块的末尾处理,这可能是 几乎立即。
如果您将其更改为实例变量,并且在没有 using
构造的情况下创建,那么它将保留在内存中。
您稍后可以 Dispose()
如果需要的话。
public partial class Form1 : Form
{
private SpeechRecognizer recognizer; // <-- instance variable
public async Task SpeechContinuousRecognitionAsync()
{
var config = SpeechConfig.FromSubscription("api key", "westus");
// Create a speech recognizer from microphone.
recognizer = new SpeechRecognizer(config);
recognizer.Recognizing += (s, e) =>
{
// ...
};
// ... rest of initialization
}
}
我有这个 c# winform,它使用 azure speech to text 将语音转换为文本。此 winform 有一个语音 on/off 复选框并执行连续语音识别,直到我关闭 window 。问题是,当我选中复选框时,只生成会话启动事件,没有其他任何事情发生,没有生成其他事件,如识别、识别、取消。你知道出了什么问题吗?
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 Microsoft.CognitiveServices.Speech;
namespace CsharpSTTform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) await SpeechContinuousRecognitionAsync();
}
public async Task SpeechContinuousRecognitionAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("api key", "westus");
// Creates a speech recognizer from microphone.
using (var recognizer = new SpeechRecognizer(config))
{
// Subscribes to events.
recognizer.Recognizing += (s, e) =>
{
Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
};
recognizer.Recognized += (s, e) =>
{
var result = e.Result;
Console.WriteLine($"Reason: {result.Reason.ToString()}");
if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"Final result: Text: {result.Text}.");
}
};
recognizer.Canceled += (s, e) =>
{
Console.WriteLine($"\n Recognition Canceled. Reason: {e.Reason.ToString()}, CanceledReason: {e.Reason}");
};
recognizer.SessionStarted += (s, e) =>
{
Console.WriteLine("\n Session started event.");
};
recognizer.SessionStopped += (s, e) =>
{
Console.WriteLine("\n Session stopped event.");
};
// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
}
}
}
}
当我选中复选框时,我得到的初始输出是:
Session started event.
The thread 0x3274 has exited with code 0 (0x0).
一段时间后我得到了这个输出:
Session started event.
The thread 0x3274 has exited with code 0 (0x0).
The thread 0x5880 has exited with code 0 (0x0).
The thread 0x9e8 has exited with code 0 (0x0).
您的 recognizer
变量是方法 SpeechContinuousRecognitionAsync
的 局部变量 ,它会在 using
块的末尾处理,这可能是 几乎立即。
如果您将其更改为实例变量,并且在没有 using
构造的情况下创建,那么它将保留在内存中。
您稍后可以 Dispose()
如果需要的话。
public partial class Form1 : Form
{
private SpeechRecognizer recognizer; // <-- instance variable
public async Task SpeechContinuousRecognitionAsync()
{
var config = SpeechConfig.FromSubscription("api key", "westus");
// Create a speech recognizer from microphone.
recognizer = new SpeechRecognizer(config);
recognizer.Recognizing += (s, e) =>
{
// ...
};
// ... rest of initialization
}
}