winform用Interop.SpeechLib.dll实现语音自动播报功能

文章目录


前言

本文使用Interop.SpeechLib.dll和System.Speech.dll实现语音自动播报

一、开发前准备

引用 Interop.SpeechLib.dll ,System.Speech.dll UI如下
在这里插入图片描述

二、开始

1.遍历播报员

正常电脑会遍历到中美2个播报员

private void Form1_Load(object sender, EventArgs e)
{
    SpVoice voice = new SpVoice();
    int co = voice.GetVoices(string.Empty, string.Empty).Count;
    if (co > 0)
    {
        List<Info> infoList = new List<Info>();
        for (int q = 0; q < co; q++)
        {
            string voicetype = voice.GetVoices(string.Empty, string.Empty).Item(q).GetDescription();
            Info zze = new Info();
            zze.Id = voicetype.Split('-')[0].ToString().Trim();
            zze.Name = voicetype.Split('-')[1].ToString().Trim();
            infoList.Add(zze);
        }
        comboBox2.DataSource = infoList;
        comboBox2.ValueMember = "Id";
        comboBox2.DisplayMember = "Name";
        comboBox2.SelectedIndex = 0;
    }
}

2.点击播报

private void button1_Click(object sender, EventArgs e)
{
    string text = richTextBox1.Text;

    if (text.Trim().Length == 0)
    {
        UIMessageTip.ShowWarning("不能阅读空内容!", 1000, true);
        return;
    }

    if (uiButton1.Text == "语音试听")
    {
        speech = new SpeechSynthesizer();
        new Thread(Speak).Start();
        uiButton1.Text = "停止试听";

    }
    else if (uiButton1.Text == "停止试听")
    {
        speech.SpeakAsyncCancelAll();//停止阅读
        uiButton1.Text = "语音试听";
    }
}

string zze = "";
private void Speak()
{
    speech.Rate = rate;//语速
    speech.SelectVoice(voicetypess);//设置播音员
    speech.Volume = value;//音量
    speech.SpeakAsync(zze);//语音阅读方法
    speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
    speech.SelectVoiceByHints(VoiceGender.Male);//声音性别
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    zze = richTextBox1.Text.ToString();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    voicetypess = comboBox2.SelectedValue.ToString();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
    value = trackBar1.Value ;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    rate = Int32.Parse(comboBox1.Text);
}
/// <summary>
/// 语音阅读完成触发此事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
    uiButton1.Invoke(new SetTextCallback(SetText), "语音试听", 1);
}


delegate void SetTextCallback(string text, int num);
private void SetText(string text, int num)
{
    uiButton1.Text = text;
}

3.生成文件

private void button2_Click(object sender, EventArgs e)
{
    string text = richTextBox1.Text;

    if (text.Trim().Length == 0)
    {
        UIMessageTip.ShowError("空内容无法生成!", 1000, true);
        return;
    }

    this.SaveFile(text);
}

/// <summary>
/// 生成语音文件的方法
/// </summary>
/// <param name="text"></param>
private void SaveFile(string text)
{
    speech = new SpeechSynthesizer();
    var dialog = new SaveFileDialog();
    dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
    dialog.ShowDialog();

    string path = dialog.FileName;
    if (path.Trim().Length == 0)
    {
        return;
    }
    speech.SetOutputToWaveFile(path);
    speech.Volume = value;
    speech.Rate = rate;
    speech.Speak(text);
    speech.SetOutputToNull();
    UINotifierHelper.ShowNotifier("生成成功!在" + path + "路径中!", UINotifierType.OK, UILocalize.SuccessTitle, false, 3000);
}

4.队列

如果需要自动轮询播报那就建一个队列类,在新建一个线程循环跑就可以了

public class QueueList
{
    Queue queuelst = new Queue();

    public void addQ(string lag)
    {
        queuelst.Enqueue(lag);
    }
    public string getQ()
    {
        string rtnMsg = "";
        if(queuelst.Count>0)
        {
            rtnMsg = queuelst.Dequeue().ToString();
        }
        return rtnMsg;
    }
}

使用方法如下

bool isSpeakCom = true;//在绑定时间完成时将这个值赋值为true 这样就会读完一条才开始读第二条
void speakLan()
{
    while(true)
    {
        if (isSpeakCom)
        {
            string lag = queueList.getQ();
            if(!string.IsNullOrWhiteSpace(lag))
            {
                isSpeakCom = false;
                speech.Rate = 0;
                speech.SelectVoice(voicetypess);//设置播音员(中文)
                speech.Volume =Convert.ToInt16(sysCurvol);
                speech.SpeakAsync(lag);//语音阅读方法
                speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
                speech.SelectVoiceByHints(VoiceGender.Male);
            }
        }
        Thread.Sleep(50);
    }
}

demo地址如下Demo下载地址

总结

以上就是自己个人的一些见解,欢迎大家留言讨论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mzzder

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值