Skip to content

Commit

Permalink
dotnet8+pause
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 27, 2024
1 parent bfb017e commit 670a4d6
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 439 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\bin
\obj
6 changes: 0 additions & 6 deletions App.config

This file was deleted.

20 changes: 8 additions & 12 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace ReadThisAloud
namespace ReadThisAloud;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

10 changes: 10 additions & 0 deletions AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
35 changes: 15 additions & 20 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,29 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ReadThisAloud"
mc:Ignorable="d"
Title="Read This Aloud" Height="80" Width="160" Topmost="True" Closing="Window_Closing" WindowStyle="ToolWindow" FontFamily="Arial" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
Title="Read This Aloud" Height="80" Width="145" Topmost="True" Closing="Window_Closing" WindowStyle="ToolWindow" FontFamily="Arial" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="#FF1A5C8C"/>
<Setter Property="Foreground" Value="#fff"/>
<Setter Property="FontSize" Value="20" />
</Style>
</Grid.Resources>
<DockPanel>
<DockPanel DockPanel.Dock="Right">
<Button x:Name="btnHelp" Width="30" DockPanel.Dock="Bottom" BorderBrush="{x:Null}"
ToolTip="Help" Click="btnHelp_Click" >
<Path Width="16" Height="16" Stretch="Fill" Fill="White"
Data="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z"/>
</Button>
<Button x:Name="btnSettings" Width="30" BorderBrush="{x:Null}" Click="btnSettings_Click"
ToolTip="Open Windows Text-to-Speech Settings" >
<Path Width="16" Height="16" Stretch="Fill" Fill="White"
Data="F1M38,23.5C38.8643,23.5,39.7109,23.5756,40.5337,23.7206L42.6275,18.5381 48.1901,20.787 46.0964,25.9692C47.6473,27.0149,48.9851,28.3527,50.0308,29.9036L55.213,27.8099 57.4619,33.3725 52.2794,35.4664C52.4244,36.2891 52.5,37.1357 52.5,38 52.5,38.8643 52.4244,39.7109 52.2794,40.5337L57.4619,42.6275 55.213,48.1901 50.0308,46.0964C49.0795,47.5073,47.8865,48.7418,46.5112,49.7405L48.7844,54.8462 43.3041,57.2891 41.0307,52.1828C40.0533,52.3906 39.0394,52.5 38,52.5 37.1357,52.5 36.2891,52.4244 35.4664,52.2794L33.3725,57.462 27.8099,55.213 29.9036,50.0309C28.3527,48.9851,27.0149,47.6473,25.9691,46.0964L20.787,48.1901 18.538,42.6275 23.7206,40.5336C23.5756,39.7109 23.5,38.8643 23.5,38 23.5,37.1357 23.5756,36.2891 23.7206,35.4664L18.538,33.3725 20.787,27.8099 25.9691,29.9036C26.9205,28.4927,28.1135,27.2582,29.4889,26.2594L27.2157,21.1537 32.6959,18.7109 34.9694,23.8172C35.9468,23.6094,36.9606,23.5,38,23.5z M38,28C32.4771,28 28,32.4772 28,38 28,43.5229 32.4771,48 38,48 43.5228,48 48,43.5229 48,38 48,32.4772 43.5228,28 38,28z"/>
</Button>
<DockPanel DockPanel.Dock="Right">
<Button x:Name="btnHelp" Width="30" FontSize="15" DockPanel.Dock="Bottom" BorderBrush="{x:Null}" ToolTip="Help" Click="btnHelp_Click" >🛈</Button>
<Button x:Name="btnSettings" Width="30" FontSize="15" BorderBrush="{x:Null}" Click="btnSettings_Click" ToolTip="Open Windows Text-to-Speech Settings" >⚙️</Button>
</DockPanel>
<Button x:Name="btnRun" Click="btnRun_Click" BorderBrush="{x:Null}" AllowDrop="True" Drop="btnRun_Drop"
ToolTip="Drag and Drop Texts Here -or- Copy Texts and Click Here">
<Grid>
<Path x:Name="StopPath" Visibility="Collapsed" Width="10" Height="10" Stretch="Fill" Fill="White" Data="M18,18H6V6H18V18Z" />
<Path x:Name="PlayPath" Width="30" Height="26" Stretch="Fill" Fill="White"
Data="M9,5A4,4 0 0,1 13,9A4,4 0 0,1 9,13A4,4 0 0,1 5,9A4,4 0 0,1 9,5M9,15C11.67,15 17,16.34 17,19V21H1V19C1,16.34 6.33,15 9,15M16.76,5.36C18.78,7.56 18.78,10.61 16.76,12.63L15.08,10.94C15.92,9.76 15.92,8.23 15.08,7.05L16.76,5.36M20.07,2C24,6.05 23.97,12.11 20.07,16L18.44,14.37C21.21,11.19 21.21,6.65 18.44,3.63L20.07,2Z"/>
</Grid>
</Button>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="btnPause" Grid.Column="0" Visibility="Collapsed" Click="btnPause_Click">⏸️</Button>
<Button x:Name="btnStop" Grid.Column="1" Visibility="Collapsed" Click="btnStop_Click">⏹️</Button>
<Button x:Name="btnRun" Grid.Column="0" Grid.ColumnSpan="2" Click="btnRun_Click" BorderBrush="{x:Null}" AllowDrop="True" Drop="btnRun_Drop" ToolTip="Drag and Drop Texts Here -or- Copy Texts and Click Here">🔊</Button>
</Grid>
</DockPanel>
</Grid>
</Window>
98 changes: 73 additions & 25 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using System.Threading;

namespace ReadThisAloud
{
public partial class MainWindow : Window
{
SpeechSynthesizer synth = new SpeechSynthesizer();
SpeechSynthesizer? synth;
System.Windows.Media.MediaPlayer player;
Dictionary<string, string> replacements = new Dictionary<string, string>();

public bool IsPlaying { get; set; }

public MainWindow()
{
InitializeComponent();
player = new System.Windows.Media.MediaPlayer();
player.Volume = 0.25;

if (File.Exists("replacements.txt"))
{
foreach (var line in File.ReadAllLines("replacements.txt"))
Expand All @@ -39,22 +39,66 @@ public MainWindow()
player.Open(new Uri(Path.GetFullPath("bg.wav")));
}

private void InitializeSynthsizer()
{
synth = new SpeechSynthesizer();
synth.SpeakCompleted += Synthsizer_SpeakCompleted;
synth.StateChanged += Synthsizer_StateChanged;
}

public void speak(string text)
{

synth?.Dispose();
player.Play();
player.MediaEnded += new EventHandler(Media_Ended);
synth.SpeakAsyncCancelAll();
foreach (var replace in replacements)
text = Regex.Replace(text, replace.Key, replace.Value);

Thread.Sleep(500); // wait a little before reading the text to avoid missing the first word on bluetooth headphones
InitializeSynthsizer();
synth.SpeakAsync(text);
synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(stop);
}
public void stop(object sender, SpeakCompletedEventArgs e)

private void Synthsizer_StateChanged(object? sender, StateChangedEventArgs e)
{
switch (e.State)
{
case SynthesizerState.Paused:
synth?.Pause();
break;
}
}

private void Synthsizer_SpeakCompleted(object? sender, SpeakCompletedEventArgs e)
{
StopAll();
}

private void btnStop_Click(object sender, RoutedEventArgs e)
{
StopAll();
}

private void Window_Closing(object sender, CancelEventArgs e)
{
PlayPath.Visibility = Visibility.Visible;
StopPath.Visibility = Visibility.Collapsed;
StopAll();
}

public void StopAll()
{
if (synth != null)
{
synth.Dispose();
synth = null;
}
player.Stop();
IsPlaying = false;
btnRun.Visibility = Visibility.Visible;
btnStop.Visibility = Visibility.Collapsed;
btnPause.Visibility = Visibility.Collapsed;
}

private void Media_Ended(object sender, EventArgs e)
{
player.Position = TimeSpan.Zero;
Expand All @@ -63,31 +107,35 @@ private void Media_Ended(object sender, EventArgs e)

private void btnRun_Click(object sender, RoutedEventArgs e)
{
if (IsPlaying)
{
synth.SpeakAsyncCancelAll();
PlayPath.Visibility = Visibility.Visible;
StopPath.Visibility = Visibility.Collapsed;
speak(Clipboard.GetText());
btnRun.Visibility = Visibility.Collapsed;
btnStop.Visibility = Visibility.Visible;
btnPause.Visibility = Visibility.Visible;
IsPlaying = true;
}

private void btnPause_Click(object sender, RoutedEventArgs e)
{
if (IsPlaying){
synth?.Pause();
btnPause.Content = "▶️";
player.Pause();
}
else
{
speak(Clipboard.GetText());
PlayPath.Visibility = Visibility.Collapsed;
StopPath.Visibility = Visibility.Visible;
else{
player.Play();
btnPause.Content = "⏸️";
Thread.Sleep(500);
synth?.Resume();
}
IsPlaying = !IsPlaying;
}

private void Window_Closing(object sender, CancelEventArgs e)
{
synth.SpeakAsyncCancelAll();
}

private void btnRun_Drop(object sender, DragEventArgs e)
{
speak(e.Data.GetData("Text") as string);
PlayPath.Visibility = Visibility.Collapsed;
StopPath.Visibility = Visibility.Visible;
btnRun.Visibility = Visibility.Collapsed;
btnStop.Visibility = Visibility.Visible;
btnPause.Visibility = Visibility.Visible;
IsPlaying = true;
}

Expand Down
55 changes: 0 additions & 55 deletions Properties/AssemblyInfo.cs

This file was deleted.

70 changes: 0 additions & 70 deletions Properties/Resources.Designer.cs

This file was deleted.

Loading

0 comments on commit 670a4d6

Please sign in to comment.