当前位置:网站首页>NET 3行代码实现文字转语音功能
NET 3行代码实现文字转语音功能
2022-07-28 09:17:00 【biyusr】
在人工智能时代,文字转语音是现在人工智能比较热门的功能,各大公司都有这方面的业务,可以通过接口对各种文字转语音,甚至能模拟真人,非常的强大,.NET东家微软其实也有这方面的服务。如果大家对语言转文字的要求不高,可以用微软自己的语音转换类库,而且实现很简单,只需要3行代码实现,本文将介绍如何使用
使用步骤
1、环境准备
新建一个控制台项目,并使用nuget工具安装:System.Speech,也可以在本地类库添加安装,如下图nuget安装

2、输入如下代码,并添加引用using System.Speech.Synthesis;
static void Main(string[] args){// 实例化 SpeechSynthesizer.SpeechSynthesizer synth = new SpeechSynthesizer();// 配置音频输出.synth.SetOutputToDefaultAudioDevice();// 字符串转语言.synth.Speak("你好!DotNeT开发跳槽");Console.WriteLine();Console.WriteLine("按任意键退出...");Console.ReadKey();}
成功朗读“你好!DotNeT开发跳槽”,这里是控制台应用,大家可以拿winfrom和wpf实现一下更加完美。
扩展实例
我们这里拿.NET网站实现一下转语音的功能,需求是输入文本框一句话,用这个System.Speech控件转换成音频,并加载到页面读取。
1、首先新建一个.NET的web应用,按上面的例子用nuget引入System.Speech
2、新建一个index.shtml页面,设计一个简单的文本框和查询按钮和一个音频输出控件。代码如下
<form><p>Title: <input type="text" asp-for="Speektext" /><input type="submit" value="生成语音" /></p></form><audio style="width:350px;height:50px;" id="bofang" controls><source src="@Model.filename" type="audio/mpeg"></audio>
3、在Index.cshtml.cs页面新建一个输入文字转换文本属性和文件路径属性,并构造函数注入读取路径中间件。
private readonly IHostingEnvironment _IhostingEnvironment;[BindProperty(SupportsGet = true)]//用来输入要转换的文本public string? Speektext { get; set; }//用来输出文件路径public string filename { get; set; }//注入用来读取存放资源文件的路径public IndexModel(IHostingEnvironment hostingEnvironment){_IhostingEnvironment= hostingEnvironment;}
4、在OnGetAsync方法中编写音频处理逻辑,代码如下:
public async Task OnGetAsync(){string wavname = "test";string filePath = _IhostingEnvironment.WebRootPath+ $"\\speech\\{wavname}.wav";bool isFile = System.IO.File.Exists(filePath);if (isFile){// 删除文件System.IO.File.Delete(filePath);}if (string.IsNullOrEmpty(Speektext))Speektext = "你好!欢迎关注“dotnet开发跳槽!”";if (!string.IsNullOrEmpty(Speektext)){using (SpeechSynthesizer synth = new SpeechSynthesizer()){//配置音频文件,设置输出流和文件格式synth.SetOutputToWaveFile(filePath, new SpeechAudioFormatInfo(32000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));// 创建空的 Prompt 对象,并为添加内容、选择语音、控件语音属性和控件朗读单词的发音提供方法PromptBuilder builder = new PromptBuilder();builder.AppendText(Speektext);//输出文件synth.Speak(builder);}//返回文件路径filename = $"\\speech\\{wavname}.wav";}}
5、在前端用js加载到音频控件。
<input type="button" id="aa" value="播放" onclick="bofang()" /><script type="text/javascript">function bofang() {var url = "@Model.filename.Replace("\\","\\\\")";var audio = document.getElementById('bofang');$('#bofang').attr('src',url);audio.play();}</script>
这样就可以用文本框输入内容,然后读取音频了,大家可以拿这个代码封装一下,改造成新闻语音朗读。效果如下:

示例代码链接:
https://pan.baidu.com/s/1IJadMleVEM3ePHE_KHqRqA?pwd=soiq
提取码:soiq
结语
本文介绍了.NET 简单的使用文本转语音的方法,大家可以参考一下,封装成自己的方法。注意本例子只支持Windows环境,在跨平台项目大家可以自己探究一下。希望本文对大家学习和工作有一定参考价值,谢谢大家的支持。
边栏推荐
- Scalable search bar, imitating Huawei application market
- FPGA development learning open source website summary
- Analysis of HashSet internal principle
- Hexadecimal representation of negative numbers
- Inside database system distributed system
- SQL Server、MySQL主从搭建,EF Core读写分离代码实现
- LeetCode_ 406_ Rebuild the queue based on height
- ARouter源码解析(三)
- Final keyword and enumeration type
- CakePHP 4.4.3 release, PHP rapid development framework
猜你喜欢
随机推荐
What is it like to use gbase C API to execute stored procedures?
什么是跨域?如何解决请跨域问题?
MySQL 8.0.30 GA
SQL server, MySQL master-slave construction, EF core read-write separation code implementation
业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)
C form application uses object binding DataGridView data binding
Introduction to shardingsphere's concept of sub database and sub table (2)
就这么一个简单的校验,80%的程序员却做不到,更不理解!
IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望
QT基础练手小程序-简单计算器设计(附带源码,解析)
Source code analysis of view event distribution mechanism
数据库高级学习笔记--游标
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
Detailed explanation of various types of files in MySQL
常用工具函数 持续更新
ECCV 2022 | can be promoted without fine adjustment! Registration based anomaly detection framework for small samples
376. 摆动序列【贪心、动态规划------】
Talk to the father of MySQL: code completion at one time is a good programmer
With such a simple verification, 80% of programmers can't do it, let alone understand it!
[autosar-rte] - introduction of 2-component, component and VFB







![ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器](/img/90/40869d7c03f09010beb989af07e2f0.png)

