C# 开发语音程序入门

本文介绍如何使用C#和Speech API (SAPI) 开发语音应用程序。适用于已安装SAPI 5.1及更高版本的Windows系统,演示了一个简单的示例项目,展示了如何使程序朗读文本框中的中文或英文文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  要想用C#开发语音程序,首先要电脑上要有Speech API(SPAI)。


大家通过读这段内容可以了解到自己的电脑是否已经安装SPAI:


The Speech API has been an integral component of all Microsoft Windows


versions since Windows 98. Microsoft Windows XP and Windows Server 2003


include SAPI version 5.1. Windows Vista and Windows Server 2008 include SAPI


version 5.3, while Windows 7 includes SAPI version 5.4. Code written for SAPI 5.3 (Vista) 


will run on SAPI 5.4 (Windows 7) without recompiling.


在5.1及以上版本都是支持中、日、英三种语言的。


我们还是先来看个例子,结合小例子再来解释:


新建项目-windows窗体应用程序,名称SpeechDemo


如图:



由于我的电脑是win7,所以可以直接添加引用:如图:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;

namespace SpeechDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSpeech_Click(object sender, EventArgs e)
        {
            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpVoice Voice = new SpVoice();
            Voice.Speak(textBox1.Text, SpFlags);
            
        }
    }
}

代码很简单,只要运行在文本框中输入中文或者英语都可以正常的朗读。



关于SpVoice接口下的函数可以参见:http://msdn.microsoft.com/en-us/library/ee413476(v=vs.85)


相关帮助连接:http://www.microsoft.com/en-us/Tellme/developers/default.aspx?tab=desktop


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值