Skip to content

Commit

Permalink
Stopping speech if clicked while currently speaking
Browse files Browse the repository at this point in the history
  • Loading branch information
stegru committed Jun 1, 2020
1 parent ffcf537 commit ed92fd0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions ReadSelectedText/ReadSelectedTextApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows;
using System.Windows.Interop;
using System.Speech.Synthesis;
using System.Threading;

namespace ReadSelectedTextApp
{
Expand All @@ -14,7 +15,8 @@ public partial class MainWindow : Window
{
private WindowInteropHelper nativeWindow;
private SelectionReader selectionReader;
private IntPtr environmentWindow;
private readonly IntPtr environmentWindow;
private readonly SpeechSynthesizer speech = new SpeechSynthesizer();

public MainWindow()
{
Expand All @@ -40,10 +42,16 @@ private async void Button_Click(object sender, RoutedEventArgs e)

private async Task SaySelection(IntPtr? activeWindow = null)
{
string text = await this.selectionReader.GetSelectedText(activeWindow);
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.SetOutputToDefaultAudioDevice();
speech.SpeakAsync(text);
if (this.speech.State != SynthesizerState.Ready)
{
this.speech.SpeakAsyncCancelAll();
}
else
{
string text = await this.selectionReader.GetSelectedText(activeWindow);
this.speech.SetOutputToDefaultAudioDevice();
this.speech.SpeakAsync(text);
}
}

private async void Init(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit ed92fd0

Please sign in to comment.