C#在windows应用程序中选择Voice not changing,但在console中选择Voice

C#在windows应用程序中选择Voice not changing,但在console中选择Voice,c#,system,selector,voice,speech,C#,System,Selector,Voice,Speech,因此,我正在尝试为System.Speech.Synthesis库更改C#中的语音。当我在控制台模式下尝试代码时,它将对我起作用。但是,当我在windows应用程序上工作时,它不会改变声音,同时不会给出任何错误。下面是windows应用程序的代码,该应用程序在语音更改中起辅助作用 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.

因此,我正在尝试为System.Speech.Synthesis库更改C#中的语音。当我在控制台模式下尝试代码时,它将对我起作用。但是,当我在windows应用程序上工作时,它不会改变声音,同时不会给出任何错误。下面是windows应用程序的代码,该应用程序在语音更改中起辅助作用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;

namespace JarvisRev1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.button1.Click += new EventHandler(button1_Click);
            this.button2.Click += new EventHandler(button2_Click);
            this.button3.Click += new EventHandler(button3_Click);

            foreach (InstalledVoice voice in sSynth.GetInstalledVoices())
            {
                cbVoice.Items.Add(voice.VoiceInfo.Name);
            }
        }

        SpeechSynthesizer sSynth = new SpeechSynthesizer();
        PromptBuilder pBuilder = new PromptBuilder();
        SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();


        private void button1_Click(object sender, EventArgs e)
        {
            pBuilder.ClearContent();
            pBuilder.AppendText(textBox1.Text);
            sSynth.SelectVoice("IVONA 2 Brian");
            sSynth.SpeakAsync(pBuilder);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = true;
            Choices sList = new Choices();
            sList.Add(new string[] { "hello", "test", "it works", "how", "are", "you", "today", "i", "am", "fine", "exit", "close", "quit", "so", "hello how are you" });
            Grammar gr = new Grammar(new GrammarBuilder(sList));
            try
            {
                sRecognize.RequestRecognizerUpdate();
                sRecognize.LoadGrammar(gr);
                sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
                sRecognize.SetInputToDefaultAudioDevice();
                sRecognize.RecognizeAsync(RecognizeMode.Multiple);
                sRecognize.Recognize();
            }

            catch
            {
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sRecognize.RecognizeAsyncStop();
            button2.Enabled = true;
            button3.Enabled = false;
        }

        private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {


            if (e.Result.Text == "exit")
            {
                Application.Exit();
            }
            else
            {
                textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
            }

        }

    }
}
这是在控制台模式下为我工作的代码

using System;
using System.Speech.Synthesis;  // Add reference to System.Speech

class Program
{
    static void Main(string[] args)
    {
        var synth = new SpeechSynthesizer();
        synth.SelectVoice("IVONA 2 Brian");
        synth.SpeakAsync("For you Sir, Always.");
        foreach (var voice in synth.GetInstalledVoices())
        {
            Console.WriteLine(voice.VoiceInfo.Name);
        }
        Console.ReadLine();
    }
}

当Microsoft Irina桌面语音在系统中可用时,也会出现同样的问题。在提示中明确设置语音的变通方法,例如:

using System.Speech.Synthesis;

var synth=new SpeechSynthesizer();
var builder=new PromptBuilder();
builder.StartVoice("Microsoft David Desktop");
builder.AppendText("Hello, World!");
builder.EndVoice();
synth.SpeakAsync(new Prompt(builder));

由于您已经使用PromptBuilder,请尝试在文本周围添加StartVoiceEndVoice调用。

不知道问题出在哪里?在此代码中,cbVoice不适用于每个(sSynth.GetInstalledVoices()中的InstalledVoice){
cbVoice
.Items.add(voice.VoiceInfo.Name); }