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窗體應用程序,文本程序需要把頁面設成異步

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM