• 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窗体应用程序,文本程序需要把页面设成异步

  • 相关阅读:
    设置sudo的过期时间
    linux下tar.xz结尾文件的解压方法
    linux版本查看命令
    linux删除目录下所有文件,但是保留文件夹
    linux下更改文件夹名
    vim中跳到第一行和最后一行
    linux下编译安装python
    linux下 python源码包解压报错
    [Swift A]
    【构建Android缓存模块】(一)吐槽与原理分析
  • 原文地址:https://www.cnblogs.com/lcidy/p/10143410.html
Copyright © 2020-2023  润新知