个人资料

主页 博客 留言板 搜索
跳过导航链接首页 > 博客列表 > 博客正文

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/1 18:21:24 最近更新2017/12/1 18:21:24 1222
为此篇作品打分
10