UWP语音朗读和语音识别

朗读

        //朗读选中文本
        private async void OnRead(object sender, RoutedEventArgs e)
        {
            string Text2Read = redit.Document.Selection.Text;
            if (Text2Read != "")
            {
                SpeechSynthesizer synthesizer = new SpeechSynthesizer();
                SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync(Text2Read);
                //Me为MediaElement AutoPlay true;
                Me.SetSource(stream,stream.ContentType);
            }
        }

识别

        //识别语音输入
        public async Task<string> BegiRecongnize()
        {
            string Result = "";

            try
            {
                using (SpeechRecognizer recognizer = new SpeechRecognizer())
                {
                    SpeechRecognitionCompilationResult compilationResult = await recognizer.CompileConstraintsAsync();
                    if (compilationResult.Status == SpeechRecognitionResultStatus.Success)
                    {

                        recognizer.UIOptions.IsReadBackEnabled = false;
                        recognizer.UIOptions.ShowConfirmation = false;
                        recognizer.UIOptions.AudiblePrompt = "我在听,请说...";
                        SpeechRecognitionResult recognitionResult = await recognizer.RecognizeWithUIAsync();
                        //SpeechRecognitionResult recognitionResult = await recognizer.RecognizeAsync();
                        if (recognitionResult.Status == SpeechRecognitionResultStatus.Success)
                        {
                            Result = recognitionResult.Text;
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                Result = ex.Message;
            }
            
           
            return Result;
        }


作者:songshizhao 发表于:2017-12-01T06:21:24 访问量:793
评论 [发表评论]