Windows Phone 8 新增功能:TTS文字朗讀功能 和 語音識別 API
一:Windows Phone 8 提供了TTS文字朗讀功能API介面
需要新增 ID_CAP_SPEECH_RECOGNITION 能力,
只需要簡單的兩段程式碼就可以完成文字輸出,
- SpeechSynthesizer synthesizer = new SpeechSynthesizer();
- await synthesizer.SpeakTextAsync("好好學習,天天向上");
二: Windows Phone 8 提供了語音識別內嵌程式內的API
需要新增 ID_CAP_SPEECH_RECOGNITION ID_CAP_NETWORKING ID_CAP_MICROPHONE 三種能力,
對於app既可以通過類 SpeechRecognizerUI 直接呼叫系統語音控制元件,也可以通過類 SpeechRecognizer 和 SpeechSynthesizer 類的組合實現自定義控制元件
參考程式碼來自官方例子 Short message dictation and web search grammars sample
- /*
- Copyright (c) 2012 Microsoft Corporation. All rights reserved.
- Use of this sample source code is subject to the terms of the Microsoft license
- agreement under which you licensed this sample source code and is provided AS-IS.
- If you did not accept the terms of the license agreement, you are not authorized
- to use this sample source code. For the terms of the license, please see the
- license agreement between you and Microsoft.
- To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604
- */
- using Microsoft.Phone.Controls;
- using System;
- using System.Windows;
- using Windows.Phone.Speech.Recognition;
- namespace sdkSpeechPredefinedGrammarsWP8CS
- {
- public partial class MainPage : PhoneApplicationPage
- {
- SpeechRecognizerUI speechRecognizer;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- this.speechRecognizer = new SpeechRecognizerUI();
- }
- private async void btnShortMessageDictation_Click(object sender, RoutedEventArgs e)
- {
- this.speechRecognizer.Recognizer.Grammars.Clear();
- // Use the short message dictation grammar with the speech recognizer.
- this.speechRecognizer.Recognizer.Grammars.AddGrammarFromPredefinedType("message", SpeechPredefinedGrammar.Dictation);
- await this.speechRecognizer.Recognizer.PreloadGrammarsAsync();
- try
- {
- // Use the built-in UI to prompt the user and get the result.
- SpeechRecognitionUIResult recognitionResult = await this.speechRecognizer.RecognizeWithUIAsync();
- if (recognitionResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
- {
- // Output the speech recognition result.
- txtDictationResult.Text = "You said: " + recognitionResult.RecognitionResult.Text;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private async void btnWebSearch_Click(object sender, RoutedEventArgs e)
- {
- this.speechRecognizer.Recognizer.Grammars.Clear();
- this.speechRecognizer.Recognizer.Grammars.AddGrammarFromPredefinedType("search", SpeechPredefinedGrammar.WebSearch);
- await this.speechRecognizer.Recognizer.PreloadGrammarsAsync();
- try
- {
- // Use the built-in UI to prompt the user and get the result.
- SpeechRecognitionUIResult recognitionResult = await this.speechRecognizer.RecognizeWithUIAsync();
- if (recognitionResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
- {
- // Output the speech recognition result.
- this.txtWebSearchResult.Text = "You said: " + recognitionResult.RecognitionResult.Text;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
參考程式碼來自官方例子 Speech recognition and text-to-speech sample
- /*
- Copyright (c) 2012 Microsoft Corporation. All rights reserved.
- Use of this sample source code is subject to the terms of the Microsoft license
- agreement under which you licensed this sample source code and is provided AS-IS.
- If you did not accept the terms of the license agreement, you are not authorized
- to use this sample source code. For the terms of the license, please see the
- license agreement between you and Microsoft.
- To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604
- */
- using Microsoft.Phone.Controls;
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Media;
- using Windows.Foundation;
- using Windows.Phone.Speech.Recognition;
- using Windows.Phone.Speech.Synthesis;
- namespace sdkSpeechColorChangeWP8CS
- {
- public partial class MainPage : PhoneApplicationPage
- {
- Dictionary<string, SolidColorBrush> _colorBrushes;
- SpeechSynthesizer synthesizer;
- SpeechRecognizer recognizer;
- IAsyncOperation<SpeechRecognitionResult> recoOperation;
- bool _isNew = true;
- bool recoEnabled = false;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- }
- protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
- {
- // On first run, set up the dictionary of SolidColorBrush objects.
- if (_isNew)
- {
- _colorBrushes = new Dictionary<string, SolidColorBrush>();
- _colorBrushes.Add("red", new SolidColorBrush(Colors.Red));
- _colorBrushes.Add("cyan", new SolidColorBrush(Colors.Cyan));
- _colorBrushes.Add("blue", new SolidColorBrush(Colors.Blue));
- _colorBrushes.Add("yellow", new SolidColorBrush(Colors.Yellow));
- _colorBrushes.Add("orange", new SolidColorBrush(Colors.Orange));
- _colorBrushes.Add("fire color", new SolidColorBrush(Colors.Magenta));
- _colorBrushes.Add("purple", new SolidColorBrush(Colors.Purple));
- _colorBrushes.Add("black", new SolidColorBrush(Colors.Black));
- _colorBrushes.Add("jet black", new SolidColorBrush(Colors.Black));
- _colorBrushes.Add("green", new SolidColorBrush(Colors.Green));
- _colorBrushes.Add("white", new SolidColorBrush(Colors.White));
- _colorBrushes.Add("dark gray", new SolidColorBrush(Colors.DarkGray));
- _colorBrushes.Add("brown", new SolidColorBrush(Colors.Brown));
- _colorBrushes.Add("magenta", new SolidColorBrush(Colors.Magenta));
- _colorBrushes.Add("gray", new SolidColorBrush(Colors.Gray));
- _isNew = false;
- }
- try
- {
- // Create the speech recognizer and speech synthesizer objects.
- if (this.synthesizer == null)
- {
- this.synthesizer = new SpeechSynthesizer();
- }
- if (this.recognizer == null)
- {
- this.recognizer = new SpeechRecognizer();
- }
- // Set up a list of colors to recognize.
- this.recognizer.Grammars.AddGrammarFromList("Colors", new List<string>() { "red", "cyan", "blue", "yellow", "orange", "fire color", "purple", "black", "jet black", "green", "white", "dark gray", "brown", "magenta", "gray" });
- }
- catch (Exception err)
- {
- txtResult.Text = err.ToString();
- }
- base.OnNavigatedTo(e);
- }
- private async void btnContinuousRecognition_Click(object sender, RoutedEventArgs e)
- {
- // Change the button text.
- if (this.recoEnabled)
- {
- this.recoEnabled = false;
- this.btnContinuousRecognition.Content = "Start speech recognition";
- txtResult.Text = String.Empty;
- this.recoOperation.Cancel();
- return;
- }
- else
- {
- this.recoEnabled = true;
- this.btnContinuousRecognition.Content = "Cancel speech recognition";
- }
- while (this.recoEnabled)
- {
- try
- {
- // Perform speech recognition.
- this.recoOperation = recognizer.RecognizeAsync();
- var recoResult = await this.recoOperation;
- // Check the confidence level of the speech recognition attempt.
- if ((int)recoResult.TextConfidence < (int)SpeechRecognitionConfidence.Medium)
- {
- // If the confidence level of the speech recognition attempt is low,
- // ask the user to try again.
- txtResult.Text = "Not sure what you said, please try again.";
- await synthesizer.SpeakTextAsync("Not sure what you said, please try again");
- }
- else
- {
- // Output that the color of the rectangle is changing by updating
- // the TextBox control and by using text-to-speech (TTS).
- txtResult.Text = "Changing color to: " + recoResult.Text;
- await synthesizer.SpeakTextAsync("Changing color to " + recoResult.Text);
- // Set the fill color of the rectangle to the recognized color.
- rectangleResult.Fill = getBrush(recoResult.Text.ToLower());
- }
- }
- catch (System.Threading.Tasks.TaskCanceledException)
- {
- // Ignore the cancellation exception of the recoOperation.
- }
- catch (Exception err)
- {
- // Handle the speech privacy policy error.
- const int privacyPolicyHResult = unchecked((int)0x80045509);
- if (err.HResult == privacyPolicyHResult)
- {
- MessageBox.Show("You must accept the speech privacy policy to continue.");
- this.recoEnabled = false;
- this.btnContinuousRecognition.Content = "Start speech recognition";
- }
- else
- {
- txtResult.Text = "Error: " + err.Message;
- }
- }
- }
- }
- /// <summary>
- /// Returns a SolidColorBrush that matches the recognized color string.
- /// </summary>
- /// <param name="reco">The recognized color string.</param>
- /// <returns>The matching colored SolidColorBrush.</returns>
- private SolidColorBrush getBrush(string recognizedColor)
- {
- if (_colorBrushes.ContainsKey(recognizedColor))
- return _colorBrushes[recognizedColor];
- return new SolidColorBrush(Colors.White);
- }
- }
- }
相關文章
- Windows Phone 8 新增功能:Windows.Storage新的檔案操作型別Windows型別
- Windows Phone 8 新增功能:解鎖開發者手機和新的除錯功能Windows除錯
- Windows Phone 8 新增功能:對SD卡的訪問WindowsSD卡
- 文字到語音(tts)TTS
- Windows10系統如何禁用語音識別功能Windows
- 蘋果iPhone XR朗讀螢幕功能怎麼用?蘋果手機文字轉語音方法教程蘋果iPhone
- PPTAutoPlay V2.0--製作具有自動語音朗讀功能的PPT
- 網頁中文字朗讀功能開發實現網頁
- win10怎麼語音讀txt文字_win10如何讓小娜語音朗讀txt文字Win10
- Windows平臺Node.js實現文字轉語音TTSWindowsNode.jsTTS
- C# TTS-文字轉語音C#TTS
- 網頁中文字朗讀功能開發實現分享網頁
- kaldi中文語音識別thchs30模型訓練程式碼功能和配置引數解讀S3模型
- Win8系統語音控制功能
- 亞馬遜將推便攜版echo音響:發力語音識別功能亞馬遜
- Win7系統怎麼開啟語音識別功能Win7
- 華為p8語音喚醒功能怎麼用 華為p8語音喚醒功能使用教程
- 百度API---語音識別API
- windows phone 8 新增功能:從一個應用程式啟動另一個程式(file association 和 Protocol association兩種方式)WindowsProtocol
- 微信小程式使用同聲傳譯實現語音識別功能微信小程式
- Windows XP語音識別技術(轉)Windows
- Windows Phone 8開發知識筆記Windows筆記
- TTS朗讀中文(using sapi sdk5) (轉)TTSAPI
- 華為機器學習服務語音識別功能,讓應用繪“聲”繪色機器學習
- MIUI 11新功能曝光:指間通話功能讓語音與文字相互轉換UI
- 圖片裁剪-文字識別-文字新增
- TTS 擂臺: 文字轉語音模型的自由搏擊場TTS模型
- 通用文字識別API-通用文字識別介面可以識別哪些場景文字API
- Windows Phone 8 新增功能:支援第三方應用建立自定義聯絡人Custom Contact Store。Windows
- 分享:哪個pdf轉換器有識別文字功能
- iOS 14.2 Beta為Control Center新增了新的Shazam音樂識別功能iOS
- 5 款不錯的開源語音識別/語音文字轉換系統
- 利用指紋識別或面部識別,為應用新增私密保護功能
- .NET 開源的功能強大的人臉識別 APIAPI
- Dribbble for windows phone 8Windows
- 簡單的python程式碼實現語音朗讀Python
- VMware vSphere 8 Update 3 新增功能
- Win10系統生物識別功能如何開啟 windows10開啟生物識別功能的步驟Win10Windows