65.9K
CodeProject 正在变化。 阅读更多。
Home

如何创建 CLI 以使用语音 API (tts) announcing 已登录用户

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2016年4月9日

CPOL

2分钟阅读

viewsIcon

8711

一个简单的基于命令行的应用程序,使用语音 API 来宣布当前已登录的用户。

引言

此代码是一个简单的命令行程序,用于宣告当前已登录用户的显示名称。 这个概念很简单,但可以很容易地扩展为宣告动态传递的数据和更复杂的逻辑。 许多示例展示了如何在窗口应用程序中使用 TTS,但很少展示 CLI 模式。

Using the Code

我使用 Windows 10 上的 Visual Studio 2013 Community Edition 按照以下步骤创建了这个程序。

在 Visual Studio 中

  • 创建一个名为 SayIt 的新项目
  • 在解决方案资源管理器中,添加引用 以下项:
    • System.Speech
    • System.DirectoryServices.AccountManagement
  • 将以下内容添加到 SayIt.cs 文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.DirectoryServices.AccountManagement;

namespace SayIt
{
    class SayIt
    {
       static void Main(string[] args)
       {
           const string voiceArg = "v:";
           const string loudArg = "l:";
           const string rateArg = "r:";

           IEnumerable<string> parsedArgsVoice = from str in args
                                                 where str.IndexOf(voiceArg) >= 0
                                                 select str.Substring(2);

           IEnumerable<int> parsedArgsLoud = from str in args
                                             where str.IndexOf(loudArg) >= 0
                                             select Int32.Parse(str.Substring(2));

           IEnumerable<int> parsedArgsRate = from str in args
                                             where str.IndexOf(rateArg) >= 0
                                             select Int32.Parse(str.Substring(2));

           SpeechSynthesizer synth = new SpeechSynthesizer();

           List<string> voiceList = new List<string>();
           foreach (var voice in synth.GetInstalledVoices())
           {
               voiceList.Add(voice.VoiceInfo.Name);
           }            

           synth.SelectVoice(parsedArgsVoice.Count() > 0
                             ? parsedArgsVoice.ElementAt(0)
                             : voiceList.ElementAt(0));

           synth.Volume = parsedArgsLoud.Count() > 0
                          ? parsedArgsLoud.ElementAt(0)
                          : 100;

           synth.Rate = parsedArgsRate.Count() > 0
                        ? parsedArgsRate.ElementAt(0)
                        : 1;

           synth.Speak("Hello " + UserPrincipal.Current.DisplayName);
       }
   }
}

上面的代码非常简单,可以分解为几个基本步骤

  1. 将已知的 CLI 参数前缀设置为 const string
  2. 使用 Linq 来解析已知的命令行参数,这样可以指定它们的顺序。 为了专注于主要逻辑,这里省略了很多类型验证代码!
  3. 创建一个新的 SpeechSynthesizer 实例
  4. 使用 SpeechSynthesizer 实例获取已安装的语音并将其存储在列表中
  5. 选择一个语音,设置音量,并在 SpeechSynthesizer 实例上设置语速。
    • 使用条件三元运算符将语音、语速和响度设置为传入的 CLI 参数或默认值(如果未作为参数传递)。
    • 传入的 Voice 必须与已安装的语音完全匹配 - 控制面板中的 TTS 设置将列出已安装的语音
    • 音量/响度的有效范围为 0 到 100
    • 语速的有效范围为 0 到 10
  6. SpeechSynthesizer 实例上调用 Speak 方法,并传递 string Hello 以及 UserPrincipal.Current.DisplayName(当前登录用户的DisplayName) 。
    • 如果希望仅使用 username,则追加 Environment.UserName 而不是 UserPrincipal.Current.DisplayName
      • 此外,可以删除 System.DirectoryServices.AccountManagement 引用和 using 语句。
      • 使用 Environment.UserName 似乎也更快。

生成解决方案后,打开命令提示符,并将目录更改为 SayIt.exe 的输出位置,然后键入以下一些命令

  • SayIt - 以默认语音、语速和响度宣告用户名
  • SayIt l:60 - 以默认语音、语速和响度 60 宣告用户名
  • SayIt r:5 l:70 - 以默认语音、语速 5 和响度 70 宣告用户名
  • SayIt l:90 r:2 "v:Microsoft Zira Desktop" - 使用 Microsoft Zira Desktop 语音、语速 2 和响度 90 宣告用户名

关注点

我在 Windows 上使用 Kinect V2 设置,并结合 Windows Hello 人脸识别进行登录。 我设置了一个任务,在登录时执行此程序。 所以我的孩子们坐在电脑前,Windows Hello 识别他们并自动登录,登录后,电脑 TTS 语音宣告 Hello Robert。 他们对此感到很兴奋。 当使用更快的 Environment.UserName 时,它通常会在登录屏幕仍在进行中时宣告,这也很酷。

历史

  • 2016-04-09 - 初始发布
© . All rights reserved.