嗨,也许这可以帮助你一个简单的 bing 语音 api 示例,这不是 winform 这是用于 WPF 应用程序 C#
using Microsoft.CognitiveServices.SpeechRecognition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.ProjectOxford.SpeechRecognition;
using System.Threading;
using System.Configuration;
namespace BingSpeech
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
AutoResetEvent _FinalResponceEvent;
MicrophoneRecognitionClient _microphoneRecognitionClient;
public MainWindow()
{
InitializeComponent();
RecordButton.Content = "Start\nRecording";
_FinalResponceEvent = new AutoResetEvent(false);
OutputTextbox.Background = Brushes.White;
OutputTextbox.Foreground = Brushes.Black;
}
private void RecordButton_Click(object sender, RoutedEventArgs e)
{
RecordButton.Content = "Listening ...";
RecordButton.IsEnabled = false;
OutputTextbox.Background = Brushes.Green;
OutputTextbox.Foreground = Brushes.White;
ConvertSpeechToText();
}
private void ConvertSpeechToText()
{
var speechRecognitionMode = SpeechRecognitionMode.ShortPhrase;
string language = "en-us";
string subscriptionKey = ConfigurationManager.AppSettings["MicrosoftSpeechApiKey"].ToString();
_microphoneRecognitionClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(
speechRecognitionMode,
language,
subscriptionKey
);
_microphoneRecognitionClient.OnPartialResponseReceived += OnPartialResponseReceivedHandler;
_microphoneRecognitionClient.OnResponseReceived += OnMicShortPhraseResponceReceivedHandler;
_microphoneRecognitionClient.StartMicAndRecognition();
}
private void OnPartialResponseReceivedHandler(object sender, PartialSpeechResponseEventArgs e)
{
string result = e.PartialResult;
jarvis.SpeakAsync(e.PartialResult);
Dispatcher.Invoke(() =>
{
OutputTextbox.Text = (e.PartialResult);
OutputTextbox.Text += ("\n");
});
}
private void OnMicShortPhraseResponceReceivedHandler(object sender, SpeechResponseEventArgs e)
{
Dispatcher.Invoke((Action)(() =>
{
_FinalResponceEvent.Set();
_microphoneRecognitionClient.EndMicAndRecognition();
_microphoneRecognitionClient.Dispose();
_microphoneRecognitionClient = null;
RecordButton.Content = "Start\nRecording";
RecordButton.IsEnabled = true;
OutputTextbox.Background = Brushes.White;
OutputTextbox.Foreground = Brushes.Black;
}));
}
}
}