private async void Listen_Click(object sender, RoutedEventArgs e)
{
if(mediaElement.CurrentState == MediaElementState.Playing)
{
mediaElement.Stop();
}
try
{
if (!recognitionActive)
{
recognitionActive = true;
var aiResponse = await AIService.StartRecognitionAsync();
recognitionActive = false;
if (aiResponse != null)
{
ProcessResult(aiResponse);
}
}
else
{
AIService.Cancel();
}
}
catch (OperationCanceledException)
{
recognitionActive = false;
resultTextBlock.Text = "Cancelled";
}
catch (Exception ex)
{
recognitionActive = false;
Debug.WriteLine(ex.ToString());
resultTextBlock.Text = $"Empty or error result: {Environment.NewLine}{ex}";
}
finally
{
listenButton.Content = "Listen";
}
}