【问题标题】:Using TTS to tell user when the stopwatch reaches a certain time使用 TTS 告诉用户秒表何时到达某个时间
【发布时间】:2015-09-21 14:31:33
【问题描述】:

我为工作编写了一个简单的秒表应用程序,我的老板希望它在您到达某个时间时告诉您,即在 15 分钟时说“15 分钟已经过去。”,在 30 分钟时说“30 分钟已经过去。” "等。我在网上查看了 TTS 的实现,但是我对如何将它实现到我现有的应用程序中持空白。我相信我对它的工作原理有很好的理解,但正如之前提到的,我对如何将它集成到我的应用程序中持空白。该应用程序是一个 Windows 窗体应用程序。任何帮助将不胜感激。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace CiscoSw
{
    public partial class SwForm : Form
    {


        private readonly Stopwatch _sw = new Stopwatch();

        public SwForm()
        {
            InitializeComponent();
            startBtn.Text = @"Start";
            UpdateDisplay();
        }

        private void SwForm_Load(object sender, EventArgs e)
        {
            currentTimeUtc.Start();
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            if (!_sw.IsRunning)
            {
                _sw.Start();
                stopwatchTimer.Start();
                startBtn.Text = @"Stop";
                UpdateDisplay();
            }
            else
            {
                _sw.Stop();
                stopwatchTimer.Stop();
                startBtn.Text = @"Start";
            }
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
            if (_sw.IsRunning)
            {
                _sw.Restart();
            }
            else
            {
                _sw.Reset();
            }
            UpdateDisplay();
        }

        private void stopwatchTimer_Tick(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            stopwatchTimeLabel.Text =
                $"{_sw.Elapsed.Hours.ToString("00")}:{_sw.Elapsed.Minutes.ToString("00")}:{_sw.Elapsed.Seconds.ToString("00")}";
        }

        private void currentTimeUtc_Tick(object sender, EventArgs e)
            {
            DateTime timenow = DateTime.UtcNow;
            displayCurrentTime.Text = $"{timenow:M/d/yyyy   HHmm Zulu}";
        }

    }

}

【问题讨论】:

  • 你想在标签中显示秒表的值吗??
  • 注意:既然您使用的是字符串插值,那么如何明智地使用它呢? $"{_sw.Elapsed.Hours:00}...
  • Kyle 秒表确实显示在标签中,需要发生的是,当秒表标签的计时器达到 15 分钟时,通过 TTS 通知用户。
  • Sami,我以为我确实正确使用了字符串插值。请指教我做错了什么。

标签: c#


【解决方案1】:

在您的项目中引用以下程序集:

System.Speech.dll 

在您的更新显示中,您必须检查间隔(15m、30m)并执行 TTS。为此,您需要实现以下内容:

using System.Speech.Synthesis;

var synthesizer = new SpeechSynthesizer(); // create this only once for reuse

synthesizer.Speak("the time interval that has lapsed"); 

如果你的电脑上没有安装微软语音,可以here下载。

您可以通过以下方式配置语音识别和 TTS(语音等):

Control Panel\All Control Panel Items\Speech Recognition

【讨论】:

  • 感谢您非常全面的回复。我已经绞尽脑汁好几个星期想弄清楚这一点。再次感谢您。
  • @JonP,不客气。请将答案标记为正确,否则会员仍会认为您的问题没有答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多