C#实现文本转语音功能
作者:qq_32915337 时间:2021-07-07 20:20:54
由于最近的工作需要用到文本转语音的功能,在网上找到的资料有些不完整,特此记录下整个完整功能。
这种方式的优点在于不会被浏览器限制,在js的文本转语音功能中,谷歌高版本的浏览器会阻止通过模拟点击的自动播放,而ie不会阻止.
一.确认研发环境
操作系统:win10或win7(我自己用的是win10 据说有些 * 版的win7会报错)
IDE:VS2012 (可高于此版本)
.NET framework 4.0(可高于此版本)
二.系统自带语音识别功能
1.C:\Windows文件夹下有Speech
2.控制面板有语音识别
三.DLL引用
1.选中要使用该功能的程序右键选择"添加引用"
2.选中"程序集"--"框架"下的System.Speech
四.代码
需要注意的是:
1.页面需要设置为异步
2.通过委托代理的方式调用,防止页面无响应
3.页面代码如下:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yy.aspx.cs" Inherits="yy" Async="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>文字转语音测试</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
aspx.cs:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Speech.Synthesis;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class yy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 文字转语音
/// </summary>
/// <param name="content">语音内容</param>
delegate void MyDelegate(string content);
string content = "有新的订单,请及时处理";
SpeechSynthesizer synthesizer = new SpeechSynthesizer(); //点击开始按钮
//开始朗读
private void speakParagh(string text)
{
synthesizer.Speak(text);
}
//朗读结束后释放资源
private void Completed(IAsyncResult result)
{
synthesizer.SpeakAsyncCancelAll();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
MyDelegate myDelegate = new MyDelegate(speakParagh); //异步调用委托
myDelegate.BeginInvoke(content, new AsyncCallback(Completed), null); //在启动异步线程后,主线程可以继续工作而不需要等待
}
catch (Exception ex)
{
Console.WriteLine("报错:" + ex.Message);
}
}
}
来源:https://blog.csdn.net/qq_32915337/article/details/123404503
标签:C#,文本,语音
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring MVC学习教程之视图深入解析
2021-12-16 23:37:55
C#图像处理之边缘检测(Sobel)的方法
2022-05-12 02:05:50
Java实现简单的日历界面
2021-10-08 03:13:01
![](https://img.aspxhome.com/file/2023/7/88147_0s.jpg)
C#实现json的序列化和反序列化实例代码
2022-07-18 01:50:34
java为何不能多继承的原因详解
2023-10-12 04:45:00
Java中MyBatis Plus知识点总结
2023-08-11 14:12:04
Java实现添加,读取和删除Excel图片的方法详解
2023-11-27 06:29:33
![](https://img.aspxhome.com/file/2023/8/60328_0s.png)
Spring Cloud如何使用Feign构造多参数的请求
2023-11-03 00:18:31
深入浅析SpringBoot自动配置原理
2022-11-19 01:49:24
![](https://img.aspxhome.com/file/2023/8/71598_0s.jpg)
Java实现十进制与二进制互转的示例详解
2022-04-02 07:40:56
简单了解Spring中常用工具类
2021-09-24 10:44:28
Java递归来实现汉诺塔游戏,注释详细
2023-07-05 10:33:52
![](https://img.aspxhome.com/file/2023/2/64412_0s.jpg)
java并发高的情况下用ThreadLocalRandom来生成随机数
2022-10-30 12:42:03
![](https://img.aspxhome.com/file/2023/6/69076_0s.jpg)
解决springboot URL带有斜杠的转义字符百分之2F导致的400错误
2022-05-08 07:27:32
![](https://img.aspxhome.com/file/2023/3/75703_0s.png)
Java基础之Spring5的核心之一IOC容器
2022-06-04 07:44:14
![](https://img.aspxhome.com/file/2023/2/81192_0s.png)
Flutter Widgets MediaQuery控件屏幕信息适配
2023-06-29 04:48:21
![](https://img.aspxhome.com/file/2023/2/120092_0s.jpg)
VMware虚拟机下hadoop1.x的安装方法
2023-07-27 08:01:40
Android在类微信程序中实现蓝牙聊天功能的示例代码
2023-09-07 06:36:19
![](https://img.aspxhome.com/file/2023/4/87304_0s.jpg)
浅谈一下Spring中的createBean
2023-06-08 22:01:26
![](https://img.aspxhome.com/file/2023/0/76620_0s.jpg)
Java设计模式之Builder建造者模式
2021-12-16 07:21:53
![](https://img.aspxhome.com/file/2023/0/68180_0s.png)