.NET 文本转语音量

.NET Text To Speech Volume(.NET 文本转语音量)
本文介绍了.NET 文本转语音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Speech.Synthesis 参考处理一个简单的 Text to Speech 应用程序.我想在应用程序中添加一个滑块控件并用它来控制语音的音量.为了设置我正在使用的音量:

I am working with a simple Text to Speech application using the System.Speech.Synthesis reference. I would like to add a slider control to the application and control the volume of the speech with it. In order to set the volume I'm using:

speech.Volume = 100;

我是否需要使用某种事件处理程序来更新此值?顺便说一句,我正在使用 C# 将其创建为 WPF 应用程序(请不要使用 VB.NET 代码).

Do I need to use some kind of event handler in order to update this value? By the way I'm creating this as a WPF application with C# (please not VB.NET code).

推荐答案

添加两个滑块,sliderVolume 用于音量控制,sliderRate 用于速率控制.然后在 SpeakProgress 事件中,为 speech 分配新的音量和速率,并使用 characterPosition 制作原始阅读内容的子字符串.然后使用这个新的子字符串重新开始说话.请参阅以下代码.

Add two sliders, sliderVolume for Volume control and sliderRate for Rate control. Then in SpeakProgress event, assign new volume and rate to speech and by using characterPosition make a sub-string of original reading content. Then restart speak using this new sub-string. See the following code.

    string selectedSpeakData = "Sample Text Sample Text Sample Text Sample Text Sample Text";
    private SpeechSynthesizer speech;

    private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                speech= new SpeechSynthesizer();
                speech.SpeakProgress += new EventHandler<System.Speech.Synthesis.SpeakProgressEventArgs>(speech_SpeakProgress);
                speech.SpeakAsync(selectedSpeakData);
            }

    void speech_SpeakProgress(object sender, System.Speech.Synthesis.SpeakProgressEventArgs e)
            {
                if (speech.Volume != Convert.ToInt32(sliderVolume.Value) || speech.Rate != Convert.ToInt32(sliderRate.Value))
                {
                    speech.Volume = Convert.ToInt32(sliderVolume.Value);
                    speech.Rate = Convert.ToInt32(sliderRate.Value);
                    selectedSpeakData = selectedSpeakData.Remove(0, e.CharacterPosition);
                    speech.SpeakAsyncCancelAll();
                    speech.SpeakAsync(selectedSpeakData);
                }
            }

这篇关于.NET 文本转语音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)