C# 文本转语音,在语音播放过程中停止语音

1,运用SpVoice播放语音

在VS2013创建Windows窗体应用程序项目,添加引用COM组件Microsoft Speech Object Library:

using SpeechLib;
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.Threading.Tasks;
using System.Windows.Forms;

namespace 测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }         
        private void button1_Click(object sender, EventArgs e)
        {
           SpVoice voice = new SpVoice();
            voice.Rate = -2; //语速,[-10,10]
            voice.Volume = 100; //音量,[0,100]
            voice.Voice = voice.GetVoices().Item(0); //语音库
            voice.Speak("hello word!");
        }  
    }
}

2,SpeechSynthesizer ,语音播放过程中停止

使用该类必须要添加引用using System.Speech.Synthesis;直接是无法添加引用的,先对项目进行添加应用

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;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SpeechSynthesizer speak = new SpeechSynthesizer(); 
        private void button1_Click(object sender, EventArgs e)
        {
            speak.Dispose();
            speak = new SpeechSynthesizer();
            speak.Volume = 100;
            speak.Rate = -1;
            speak.SpeakAsync("The company has six series of products, facing the four markets of higher education, community education, cadre education and children's education. It is a professional and comprehensive provider of digital lifelong learning solutions.");
        }       
        private void button2_Click(object sender, EventArgs e)
        {
            speak.Pause();            
    }
}

此方法适用于WindowForm窗体应用程序,文本程序需要把页面设成异步

原文地址:https://www.cnblogs.com/lcidy/p/10143410.html