WPF---語音合成與語音識別

 windows vista 和windows 7語音識別功能相關簡介:html

    相信用過windows vista 和windows 7的人都知道或者瞭解過裏面的語音識別功能。它可使用聲音命令來控制電腦,實現更方便的人機互動,還能夠經過聲音控制窗口、啓動程序、在窗口之間切換,使用菜單和單擊按鈕等功能。利用聲音讓計算機聽寫文本,只要大聲的朗讀字詞,就能夠建立文本文檔,也可在文檔中進行修改或更正錯誤。但此項技術並非很成熟,存在文本識別率不高,許多非微軟的程序不支持Windows的語音命令等缺陷。ios

    那麼在咱們的WPF程序中,該如何利用此功能呢?(目前使用改技術意義不大,可是在未來,我相信會有很大的發展,在此,並且是新手,我只是以玩的心態作這些,但願你們不要笑 :) 小程序

    在語音合成和語音識別上,微軟提供 Speech SDK 開發包,那麼在咱們的WPF程序中,咱們怎麼使用呢?vim

其實很簡單,咱們主要用到了windows

.NET Framework 類庫中

在System.Speech.Synthesis命名空間下async

程序集:  System.Speech(在 System.Speech.dll 中)函數

SpeechSynthesizer 類
  
此類中的成員包括以下:
 構造函數
  名稱 說明
ms596247.pubmethod(zh-cn,VS.90).gif SpeechSynthesizer Creates a new instance of SpeechSynthesizer.
頁首
  名稱 說明
ms596247.pubmethod(zh-cn,VS.90).gif AddLexicon
ms596247.pubmethod(zh-cn,VS.90).gif Dispose
ms596247.pubmethod(zh-cn,VS.90).gif Equals 肯定指定的 Object 是否等於當前的 Object(繼承自 Object。)
ms596247.protmethod(zh-cn,VS.90).gif Finalize 容許 Object 在「垃圾回收」回收 Object 以前嘗試釋放資源並執行其餘清理操做。 (繼承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gif GetCurrentlySpokenPrompt
ms596247.pubmethod(zh-cn,VS.90).gif GetHashCode 用做特定類型的哈希函數。 (繼承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gif GetInstalledVoices 已重載。 Returns the collection of installed TTS voices.
ms596247.pubmethod(zh-cn,VS.90).gif GetType 獲取當前實例的 Type(繼承自 Object。)
ms596247.protmethod(zh-cn,VS.90).gif MemberwiseClone 建立當前 Object 的淺表副本。 (繼承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gif Pause Pauses the synthesizer.
ms596247.pubmethod(zh-cn,VS.90).gif RemoveLexicon
ms596247.pubmethod(zh-cn,VS.90).gif Resume
ms596247.pubmethod(zh-cn,VS.90).gif SelectVoice Selects a specific voice.
ms596247.pubmethod(zh-cn,VS.90).gif SelectVoiceByHints 已重載。 Selects a voice with specific voice characteristics.
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToAudioStream
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToDefaultAudioDevice
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToNull
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToWaveFile 已重載。
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToWaveStream
ms596247.pubmethod(zh-cn,VS.90).gif Speak 已重載。 Speaks a prompt.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakAsync 已重載。 Speaks asynchronously.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakAsyncCancel Cancels asynchronous speaking of the specified prompt.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakAsyncCancelAll Cancels asynchronous speaking of all queued prompts.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakSsml Speaks the specified SSML string.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakSsmlAsync Speaks the specified text string asynchronously.
ms596247.pubmethod(zh-cn,VS.90).gif ToString 返回表示當前 ObjectString(繼承自 Object。)
頁首
  名稱 說明
ms596247.pubproperty(zh-cn,VS.90).gif Rate Gets the speaking rate of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gif State Gets the speaking state of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gif Voice Gets the voice of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gif Volume Gets the speaking volume of the SpeechSynthesizer.
頁首
  名稱 說明
ms596247.pubevent(zh-cn,VS.90).gif BookmarkReached Raised when a bookmark is reached.
ms596247.pubevent(zh-cn,VS.90).gif PhonemeReached Raised when a phoneme is reached.
ms596247.pubevent(zh-cn,VS.90).gif SpeakCompleted Raised when the SpeechSynthesizer completes the speaking of a prompt.
ms596247.pubevent(zh-cn,VS.90).gif SpeakProgress
ms596247.pubevent(zh-cn,VS.90).gif SpeakStarted Raised when the SpeechSynthesizer begins the speaking of a prompt.
ms596247.pubevent(zh-cn,VS.90).gif StateChanged Raised when the state of the SpeechSynthesizer changes.
ms596247.pubevent(zh-cn,VS.90).gif VisemeReached Raised when a viseme is reached.
ms596247.pubevent(zh-cn,VS.90).gif VoiceChange Raised when the voice of the SpeechSynthesizer changes.
 
 
    瞭解了以上相關知識,咱們就開始來作今天的小程序啦,實現一個能根據你提問:「如今幾點了?」,而後
計算機將獲取當前時間,在界面上顯示時間的同時,以語音報時。。。。實在是有點簡單,嘿嘿。。
    首先新建一個WPF Application ;
    而後在經過設計視窗在界面上拖拽兩個Lable控件和一個Button控件,結果就變成下面這個樣子了:
怎麼Button不見了?嘿嘿,被我給隱藏了,把Opcity屬性設置了0,咱們使用這個Button主要是爲了讓它得到焦點,
而且觸發它的Click事件,可是,咱們如今是懶得動手去按了,話說咱們今天要說說話去命令它。而後觸發這個事件,獲得當前時間,並作出相關反應。
整個過程就這樣簡單。因此,爲了產生神祕感,就把Button給隱藏了 :)
說了這麼多,接下來看代碼吧:
 
MainWindow.xaml
 1  < Window  x:Class ="MySpeach.MainWindow"
 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 >

 

 

 

MainWindow.xaml.cs
 1  namespace  MySpeach
 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      }

 

其實上面的程序也沒什麼可講的,無非就是幾個方法的調用,沒什麼技術含量,重要部分都已經註釋。最後,要注意的是,別忘記了ui

對System.Speech.Synthesis;命名空間和相關程序集的引用。好了,就娛樂到這裏,次程序在windows 7平臺下使用vs2010編譯經過,並能正常運行(只要你講的普通話接近標準,嘿嘿:) 最後,在運行時別忘記了打開windows自帶的語音識別程序,來進行聆聽,否則,你喊死了計算機都不來鳥你 - -!spa

相關文章
相關標籤/搜索