WPF---语音合成与语音识别
windows vista 和windows 7语音识别功能相关简介:
相信用过windows vista 和windows 7的人都知道或者了解过里面的语音识别功能。它可以使用声音命令来控制电脑,实现更方便的人机互动,还可以通过声音控制窗口、启动程序、在窗口之间切换,使用菜单和单击按钮等功能。利用声音让计算机听写文本,只要大声的朗读字词,就可以创建文本文档,也可在文档中进行修改或更正错误。但此项技术并不是很成熟,存在文本识别率不高,许多非微软的程序不支持Windows的语音命令等缺陷。
那么在我们的WPF程序中,该如何利用此功能呢?(目前使用改技术意义不大,但是在将来,我相信会有很大的发展,在此,而且是新手,我只是以玩的心态做这些,希望大家不要笑 :)
在语音合成和语音识别上,微软提供 Speech SDK 开发包,那么在我们的WPF程序中,我们怎么使用呢?
其实很简单,我们主要用到了
在System.Speech.Synthesis命名空间下
程序集: System.Speech(在 System.Speech.dll 中) 的

名称 | 说明 | |
---|---|---|
![]() |
AddLexicon | |
![]() |
Dispose | |
![]() |
Equals | 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。) |
![]() |
Finalize | 允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。 (继承自 Object。) |
![]() |
GetCurrentlySpokenPrompt | |
![]() |
GetHashCode | 用作特定类型的哈希函数。 (继承自 Object。) |
![]() |
GetInstalledVoices | 已重载。 Returns the collection of installed TTS voices. |
![]() |
GetType | 获取当前实例的 Type。 (继承自 Object。) |
![]() |
MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) |
![]() |
Pause | Pauses the synthesizer. |
![]() |
RemoveLexicon | |
![]() |
Resume | |
![]() |
SelectVoice | Selects a specific voice. |
![]() |
SelectVoiceByHints | 已重载。 Selects a voice with specific voice characteristics. |
![]() |
SetOutputToAudioStream | |
![]() |
SetOutputToDefaultAudioDevice | |
![]() |
SetOutputToNull | |
![]() |
SetOutputToWaveFile | 已重载。 |
![]() |
SetOutputToWaveStream | |
![]() |
Speak | 已重载。 Speaks a prompt. |
![]() |
SpeakAsync | 已重载。 Speaks asynchronously. |
![]() |
SpeakAsyncCancel | Cancels asynchronous speaking of the specified prompt. |
![]() |
SpeakAsyncCancelAll | Cancels asynchronous speaking of all queued prompts. |
![]() |
SpeakSsml | Speaks the specified SSML string. |
![]() |
SpeakSsmlAsync | Speaks the specified text string asynchronously. |
![]() |
ToString | 返回表示当前 Object 的 String。 (继承自 Object。) |

名称 | 说明 | |
---|---|---|
![]() |
BookmarkReached | Raised when a bookmark is reached. |
![]() |
PhonemeReached | Raised when a phoneme is reached. |
![]() |
SpeakCompleted | Raised when the SpeechSynthesizer completes the speaking of a prompt. |
![]() |
SpeakProgress | |
![]() |
SpeakStarted | Raised when the SpeechSynthesizer begins the speaking of a prompt. |
![]() |
StateChanged | Raised when the state of the SpeechSynthesizer changes. |
![]() |
VisemeReached | Raised when a viseme is reached. |
![]() |
VoiceChange | Raised when the voice of the SpeechSynthesizer changes. |

2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="Speech" Height="350" Width="525">
5 <Grid>
6 <Label Content="现在时间:" Height="32" HorizontalAlignment="Left" Margin="85,119,0,0" Name="label1" VerticalAlignment="Top" Width="81" />
7 <Label Height="30" HorizontalAlignment="Left" Margin="198,119,0,0" Name="label2" VerticalAlignment="Top" Width="185" IsEnabled="False" Visibility="Visible" />
8 <Button Content="现在现在几点了" Height="25" HorizontalAlignment="Left" Margin="198,65,0,0" Name="button1" VerticalAlignment="Top" Width="60" Opacity="0" Click="button1_Click"/>
9 </Grid>
10 </Window>
2 {
3 /// <summary>
4 /// Interaction logic for MainWindow.xaml
5 /// </summary>
6 public partial class MainWindow : Window
7 {
8 public MainWindow()
9 {
10 InitializeComponent();
11
12 button1.Focus(); //按钮获取输入焦点
13
14
15
16 }
17 public PromptBuilder BuildPB() //建立并构建PromptBuilder 对象并返回此对象
18 {
19 PromptBuilder pb=new PromptBuilder();
20 pb.StartVoice("大哥"); //构建pb对象内容
21 pb.AppendText("主人现在是北京时间");
22 pb.AppendTextWithHint(DateTime.Now.ToString("HH:MM"),SayAs.Time24);
23 pb.AppendBreak(new TimeSpan(0,0,4));
24 pb.EndVoice();
25
26
27 return pb;
28 }
29 private void button1_Click(object sender, RoutedEventArgs e)
30 {
31 label2.Content ="现在是北京时间"+DateTime.Now.ToString("HH:MM")+"分" ;
32 SpeechSynthesizer syn = new SpeechSynthesizer();
33 syn.SpeakAsync(BuildPB()); //通过调用SpeechSynthesizer对象的SpeakAsync()方法,输出语音
34 button1.Focus();
35
36 }
37 }
其实上面的程序也没什么可讲的,无非就是几个方法的调用,没什么技术含量,重要部分都已经注释。最后,要注意的是,别忘记了
对System.Speech.Synthesis;命名空间和相关程序集的引用。好了,就娱乐到这里,次程序在windows 7平台下使用vs2010编译通过,并能正常运行(只要你讲的普通话接近标准,嘿嘿:) 最后,在运行时别忘记了打开windows自带的语音识别程序,来进行聆听,不然,你喊死了计算机都不来鸟你 - -!
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构