Перекомпилировано в DotNetFramework 4.5, 4.6 *, 4,7 * и протестировано каждая подверсия, и все они отлично работают.
Это ошибка DotNetFramework 4.8, которая, по-видимому, не была сообщена / устранена в Microsoft. Снижение до 4.7.2 решило проблему
Чтобы дублировать эту проблему - вот полный и работающий код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpeechDemoApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SpeechSynthesizer _SS = new SpeechSynthesizer();
private void btnSpeak_Click(object sender, EventArgs e)
{
if (cboVoices.SelectedIndex >= 0)
{
_SS.SelectVoice(cboVoices.SelectedItem.ToString());
}
_SS.Volume = trackBar1.Value;
_SS.SpeakAsync(txtPhrase.Text.Trim());
}
private void Form2_Load(object sender, EventArgs e)
{
var ivs = _SS.GetInstalledVoices();
foreach (var iv in ivs)
{
ComboboxItem cboItem = new ComboboxItem();
cboItem.Text = iv.VoiceInfo.Name;
cboItem.Value = iv.VoiceInfo.Name;
cboVoices.Items.Add(cboItem);
}
if (cboVoices.Items.Count > 0)
{
cboVoices.SelectedIndex = 0;
}
}
private void cboVoices_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
protected override void OnClosing(CancelEventArgs e)
{
_SS.SpeakAsyncCancelAll();
base.OnClosing(e);
}
}
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
}
И разработчик:
namespace SpeechDemoApp
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cboVoices = new System.Windows.Forms.ComboBox();
this.txtPhrase = new System.Windows.Forms.TextBox();
this.btnSpeak = new System.Windows.Forms.Button();
this.trackBar1 = new System.Windows.Forms.TrackBar();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// cboVoices
//
this.cboVoices.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboVoices.FormattingEnabled = true;
this.cboVoices.Location = new System.Drawing.Point(41, 37);
this.cboVoices.Name = "cboVoices";
this.cboVoices.Size = new System.Drawing.Size(144, 21);
this.cboVoices.TabIndex = 0;
this.cboVoices.SelectedIndexChanged += new System.EventHandler(this.cboVoices_SelectedIndexChanged);
//
// txtPhrase
//
this.txtPhrase.Location = new System.Drawing.Point(41, 82);
this.txtPhrase.Name = "txtPhrase";
this.txtPhrase.Size = new System.Drawing.Size(664, 20);
this.txtPhrase.TabIndex = 1;
this.txtPhrase.Text = "This is a test of the Emergency Webcast System. If this were an actual emergency," +
" you would be bleeding from all of your orifices.";
//
// btnSpeak
//
this.btnSpeak.Location = new System.Drawing.Point(41, 127);
this.btnSpeak.Name = "btnSpeak";
this.btnSpeak.Size = new System.Drawing.Size(92, 23);
this.btnSpeak.TabIndex = 2;
this.btnSpeak.Text = "Speak";
this.btnSpeak.UseVisualStyleBackColor = true;
this.btnSpeak.Click += new System.EventHandler(this.btnSpeak_Click);
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(240, 127);
this.trackBar1.Maximum = 100;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(465, 45);
this.trackBar1.TabIndex = 3;
this.trackBar1.Value = 80;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.btnSpeak);
this.Controls.Add(this.txtPhrase);
this.Controls.Add(this.cboVoices);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox cboVoices;
private System.Windows.Forms.TextBox txtPhrase;
private System.Windows.Forms.Button btnSpeak;
private System.Windows.Forms.TrackBar trackBar1;
}
}