Windows Phone 8 新增功能:TTS文本朗讀功能 和 語音辨識 API

來源:互聯網
上載者:User

一: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);        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.