Skip to content

Commit

Permalink
新增上下课 Edge TTS 语音提醒,修改版本号为 3.2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
STBBRD committed Aug 24, 2024
1 parent 6720bc8 commit 7ef19b5
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 31 deletions.
5 changes: 2 additions & 3 deletions ZongziTEK_Blackboard_Sticker/Classes/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ public class TimetableSettings
{
public bool IsTimetableEnabled { get; set; } = true;
public bool IsTimetableNotificationEnabled { get; set; } = true;
/*public bool UseDefaultBNSPath { get; set; } = true;
public string BNSPath { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Blackboard Notification Service";*/
public double FontSize { get; set; } = 24;
public double BeginNotificationTime { get; set; } = 60;
//public double BeginNotificationPreTime { get; set; } = 10;
public bool IsBeginSpeechEnabled { get; set; } = false;
public double OverNotificationTime { get; set; } = 10;
public bool IsOverSpeechEnabled { get; set; } = false;
public double TimeOffset { get; set; } = 0; // 秒
}

Expand Down
55 changes: 55 additions & 0 deletions ZongziTEK_Blackboard_Sticker/Helpers/TTSHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Synthesis;
using System.IO;
using Edge_tts_sharp;
using System.Net.NetworkInformation;

namespace ZongziTEK_Blackboard_Sticker.Helpers
{
public static class TTSHelper
{
public static void PlayText(string text)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
EdgeTTSPlayText(text);
}
else
{
SysTTSPlayText(text);
}
}

private static void EdgeTTSPlayText(string text)
{
Task.Run(() =>
{
var voice = Edge_tts.GetVoice()[55];
Edge_tts.PlayText(text, voice);
});
}

private static void SysTTSPlayText(string text)
{
SpeechSynthesizer synthesizer = null;

try
{
synthesizer = new SpeechSynthesizer();
}
catch
{
// 系统 TTS 不存在
}

if (synthesizer != null)
{
synthesizer.SpeakAsync(text);
}
}
}
}
37 changes: 32 additions & 5 deletions ZongziTEK_Blackboard_Sticker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void iconSwitchRight_MouseDown(object sender, MouseButtonEventArgs e)
iconSwitchLeft.Visibility = Visibility.Visible;
}

/*private DispatcherTimer windowTimer = new DispatcherTimer() // 强力置底,可能导致界面闪烁,故注释
/*private DispatcherTimer windowTimer = new DispatcherTimer() // 强力置底,可能导致界面闪烁
{
Interval = new TimeSpan(0, 0, 0, 0, 500)
};
Expand Down Expand Up @@ -1278,10 +1278,10 @@ private void CheckTimetable(object sender, EventArgs e)

private void ShowClassBeginPreNotification(List<Lesson> today, int index)
{
int nextLessonIndex = index + 1;

if (Settings.TimetableSettings.IsTimetableNotificationEnabled)
{
int nextLessonIndex = index + 1;

string startTimeString = today[nextLessonIndex].StartTime.ToString(@"hh\:mm");
string endTimeString = today[nextLessonIndex].EndTime.ToString(@"hh\:mm");

Expand All @@ -1290,14 +1290,27 @@ private void ShowClassBeginPreNotification(List<Lesson> today, int index)

new TimetableNotificationWindow(title, subtitle, Settings.TimetableSettings.BeginNotificationTime, true).Show();
}

if (Settings.TimetableSettings.IsBeginSpeechEnabled)
{
int timeLeft = Convert.ToInt32(Settings.TimetableSettings.BeginNotificationTime / 60);
if (timeLeft > 0)
{
TTSHelper.PlayText("距上课还有" + timeLeft.ToString() + "分钟。" + "准备上课," + today[nextLessonIndex].Subject + "课即将开始");
}
else
{
TTSHelper.PlayText("准备上课," + today[nextLessonIndex].Subject + "课即将开始");
}
}
}

private void ShowClassOverNotification(List<Lesson> today, int index)
{
int nextLessonIndex = index + 1;

if (Settings.TimetableSettings.IsTimetableNotificationEnabled)
{
int nextLessonIndex = index + 1;

string startTimeString = today[nextLessonIndex].StartTime.ToString(@"hh\:mm");

string title = "下一节 " + today[nextLessonIndex].Subject + "课";
Expand All @@ -1311,8 +1324,15 @@ private void ShowClassOverNotification(List<Lesson> today, int index)
{
title = "下一节是 " + today[nextLessonIndex].Subject + "课";
new StrongNotificationWindow(title, subtitle).Show();
TTSHelper.PlayText("下课。下一节是" + today[nextLessonIndex].Subject + "课");
return;
}
}

if (Settings.TimetableSettings.IsOverSpeechEnabled)
{
TTSHelper.PlayText("下课。下一节是" + today[nextLessonIndex].Subject + "课");
}
}

private void ShowLastClassOverNotification(bool isStrongNotificationEnabled)
Expand All @@ -1326,8 +1346,15 @@ private void ShowLastClassOverNotification(bool isStrongNotificationEnabled)
else
{
new StrongNotificationWindow("课堂结束", "").Show();
TTSHelper.PlayText("课堂结束");
return;
}
}

if (Settings.TimetableSettings.IsOverSpeechEnabled)
{
TTSHelper.PlayText("课堂结束");
}
}

private void ScrollToCurrentLesson()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
Header="使用带时间信息的课程表" Tip="关闭后,课程表将变为纯文本,失去一切与时间有关的功能" OnContent="启用" OffContent="禁用"
IsOn="{Binding IsTimetableEnabled, Mode=TwoWay}" Toggled="ToggleSwitchUseTimetable_Toggled"/>
<ikw:SimpleStackPanel Spacing="8" Visibility="{Binding ElementName=ToggleSwitchUseTimetable, Path=IsOn, Converter={StaticResource BoolToVisibilityConverter}}">
<card:ToggleSwitchCard Icon="{x:Static ui:FluentSystemIcons.Alert_20_Regular}" Header="上下课提醒" OnContent="启用" OffContent="禁用"
<card:ToggleSwitchCard Icon="{x:Static ui:FluentSystemIcons.Alert_20_Regular}" Header="上下课提醒" Tip="此设置不会影响语音播报" OnContent="启用" OffContent="禁用"
IsOn="{Binding IsTimetableNotificationEnabled, Mode=TwoWay}" Toggled="ToggleSwitchIsTimetableNotificationEnabled_Toggled"/>
<card:ToggleSwitchCard Icon="{x:Static ui:FluentSystemIcons.PersonVoice_20_Regular}" Header="准备上课语音提醒" OnContent="启用" OffContent="禁用"
IsOn="{Binding IsBeginSpeechEnabled, Mode=TwoWay}" Toggled="ToggleSwitchIsBeginSpeechEnabled_Toggled"/>
<card:ToggleSwitchCard Icon="{x:Static ui:FluentSystemIcons.PersonVoice_20_Regular}" Header="课堂结束语音提醒" OnContent="启用" OffContent="禁用"
IsOn="{Binding IsOverSpeechEnabled, Mode=TwoWay}" Toggled="ToggleSwitchIsOverSpeechEnabled_Toggled"/>
<card:SliderCard Icon="{x:Static ui:FluentSystemIcons.ArrowEnter_20_Regular}" Header="课前通知持续时间(秒)" Minimum="20" Maximum="180" TickFrequency="2.5"
Value="{Binding BeginNotificationTime, Mode=TwoWay}" ValueChanged="SliderBeginNotificationTime_ValueChanged"/>
<card:SliderCard Icon="{x:Static ui:FluentSystemIcons.PersonRunning_20_Regular}" Header="下课通知持续时间(秒)" Minimum="4" Maximum="60" TickFrequency="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,15 @@ private void SliderTimeOffset_ValueChanged(object sender, RoutedEventArgs e)
{
MainWindow.SaveSettings();
}

private void ToggleSwitchIsBeginSpeechEnabled_Toggled(object sender, RoutedEventArgs e)
{
MainWindow.SaveSettings();
}

private void ToggleSwitchIsOverSpeechEnabled_Toggled(object sender, RoutedEventArgs e)
{
MainWindow.SaveSettings();
}
}
}
4 changes: 2 additions & 2 deletions ZongziTEK_Blackboard_Sticker/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.2.1.0")]
[assembly: AssemblyFileVersion("3.2.1.0")]
[assembly: AssemblyVersion("3.2.2.0")]
[assembly: AssemblyFileVersion("3.2.2.0")]
19 changes: 1 addition & 18 deletions ZongziTEK_Blackboard_Sticker/StrongNotificationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,8 @@ public StrongNotificationWindow(string title, string subtitle)
TextBlockSubtitle.Text = subtitle;

if (subtitle == "") TextBlockSubtitle.Visibility = Visibility.Collapsed;

try
{
synthesizer = new SpeechSynthesizer();
}
catch (Exception ex)
{
// 系统 TTS 不存在
}
}

private SpeechSynthesizer synthesizer = null;

private async void Window_Loaded(object sender, RoutedEventArgs e)
{
// 设置为 ToolWindow
Expand All @@ -70,13 +59,7 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
ViewboxContent.BeginAnimation(MarginProperty, viewboxMarginAnimationIn);
await Task.Delay(500);

// 语音播报
if (synthesizer != null)
{
synthesizer.Speak(TextBlockTitle.Text);
await Task.Delay(200);
synthesizer.Speak(TextBlockSubtitle.Text);
}
await Task.Delay(10000);

// 退出动画
DoubleAnimation opacityAnimationOut = new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="netstandard" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
Expand Down Expand Up @@ -102,6 +101,7 @@
<DependentUpon>TimetableLesson.xaml</DependentUpon>
</Compile>
<Compile Include="Helpers\ControlsHelper.cs" />
<Compile Include="Helpers\TTSHelper.cs" />
<Compile Include="Pages\InfoBoardPages\CountdownPage.xaml.cs">
<DependentUpon>CountdownPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -386,7 +386,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autoupdater.NET.Official">
<Version>1.9.1</Version>
<Version>1.9.2</Version>
</PackageReference>
<PackageReference Include="Edge_tts_sharp">
<Version>1.1.1</Version>
</PackageReference>
<PackageReference Include="iNKORE.UI.WPF">
<Version>1.2.7.1</Version>
Expand Down

0 comments on commit 7ef19b5

Please sign in to comment.