UWP XAML ToggleSwitch将语音合成语音设置为男性或女性

时间:2016-07-17 00:08:34

标签: c# binding uwp-xaml speech-synthesis toggleswitch

我正在尝试通过Template10 UWP应用程序设置页面中的ToggleSwitch将voice.gender设置为男性或女性。

我宣布TG:

<ToggleSwitch x:Name="VoiceSelection" Header="Select Voice"
                                  IsOn="{Binding VoiceChoice, Mode=TwoWay}"
                                  OffContent="Male Voice" OnContent="Female Voice" />

那应该没事。

然后我设置了一个布尔值,用于稍后选择男性或女性

public event PropertyChangedEventHandler PropertyChanged;
public static bool _voiceChoice = true;
public bool VoiceChoice
    {
        get
        {
            return _voiceChoice;
        }
        set
        {
            _voiceChoice = value;
            OnPropertyChanged("VoiceChoice");
        }
    }

protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

有关信息,以下是稍后分配语音的代码。这也很好。

...
if (_voiceChoice == true)
                {
                    VoiceInformation voiceInfo =
                      (
                        from voice in SpeechSynthesizer.AllVoices
                        where voice.Gender == VoiceGender.Female
                        select voice
                      ).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
                    synthesizer.Voice = voiceInfo;
                    stream = await synthesizer.SynthesizeTextToStreamAsync(text);
                }
                else
...

我遇到的问题是我可以通过手动设置布尔_voiceChoice来选择语音,但我无法通过ToggleSwitch进行设置。

我也意识到这个解决方案不是很干净,我对任何建议持开放态度。任何帮助是极大的赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

实际上,我完全错误地看待这个。
以下是使用ToggleSwitch在Template10中切换男/女声音所需的功能。可能是更清洁的解决方案,但这有效。

在SettingsPage.xaml中,添加:

<ToggleSwitch x:Name="VoiceSelection" Header="Select Voice"
    IsOn="{Binding UseVoiceSelection, Mode=TwoWay}"
    OffContent="Male Voice" OnContent="Female Voice" />

在SettingsService.cs中,添加:

public bool UseVoiceSelection
    {
        get { return _helper.Read<bool>(nameof(UseVoiceSelection), true); }
        set
        {
            _helper.Write(nameof(UseVoiceSelection), value);
        }
    }

在类SettingsPartViewModel:ViewModelBase中,添加:

public bool UseVoiceSelection
    {
        get { return _settings.UseVoiceSelection; }
        set { _settings.UseVoiceSelection = value; base.RaisePropertyChanged(); }
    }

最后,在一个单独的类中,设置bool值并执行语音合成:

public class ReadSpeech
{
    public static bool _voiceChoice = true;

    // Performs synthesis
    async Task<IRandomAccessStream> SynthesizeTextToSpeechAsync(string text)
    {
        IRandomAccessStream stream = null;
        using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
        {
            if (temp.SettingsPartViewModel.UseVoiceSelection == true)
            {
                VoiceInformation voiceInfo =
                  (
                    from voice in SpeechSynthesizer.AllVoices
                    where voice.Gender == VoiceGender.Female
                    select voice
                  ).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
                synthesizer.Voice = voiceInfo;
                stream = await synthesizer.SynthesizeTextToStreamAsync(text);
            }
            else
            {
                VoiceInformation voiceInfo =
                  (
                    from voice in SpeechSynthesizer.AllVoices
                    where voice.Gender == VoiceGender.Male
                    select voice
                  ).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
                synthesizer.Voice = voiceInfo;
                stream = await synthesizer.SynthesizeTextToStreamAsync(text);
            }
        }
        return (stream);
    }