当前位置:网站首页>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环境,在跨平台项目大家可以自己探究一下。希望本文对大家学习和工作有一定参考价值,谢谢大家的支持。
边栏推荐
- Window源码解析(一):与DecorView的那些事
- Window source code analysis (I): things with decorview
- IDC script file running
- MATLAB的实时编辑器
- MATLAB的数列与极限运算
- Business visualization - make your flowchart'run'(4. Actual business scenario test)
- [JVM] JVM refers to floating point number
- What is it like to use gbase C API to execute stored procedures?
- [Guangxi University] information sharing of postgraduate entrance examination and re examination
- CakePHP 4.4.3 release, PHP rapid development framework
猜你喜欢

Use xposed to crack the software

Introduction to SD card (based on spec3.0)
![[C language] detailed explanation sequence table (seqlist)](/img/60/c8cee6a6afe57247aba583291cc99b.png)
[C language] detailed explanation sequence table (seqlist)
![[vscode] vscode usage](/img/0d/d6edbad047ecd7a092d4d7aa06e04d.png)
[vscode] vscode usage

Network engineering -- ranking of Chinese universities in Soft Science

Introduction to shardingsphere (I)

Method parameter transfer mechanism of C #

Dn-detr paper accuracy, and analyze its model structure & 2022 CVPR paper

376. 摆动序列【贪心、动态规划------】
![ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器](/img/90/40869d7c03f09010beb989af07e2f0.png)
ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
随机推荐
IDC script file running
2022牛客多校第一场补题
Window源码解析(三):Window的更新机制
业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)
[swintransformer source code reading II] window attention and shifted window attention
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
什么是跨域?如何解决请跨域问题?
力扣376-摆动序列——贪心
【日志】日志干什么的?日志工厂是什么?log4j 的配置和使用? log4j.properties 文件配置、log4j jar包坐标
2.9.5 ext JS object type processing and convenient methods
Retrofit source code analysis
Final keyword and enumeration type
Window源码解析(二):Window的添加机制
《PyTorch深度学习实践》第九课多分类问题(手写数字MNIST)
How promise instance solves hell callback
opencv安装配置测试
MATLAB的符号运算
使用 OpenSSL 创建ssl证书
Talk to the father of MySQL: code completion at one time is a good programmer
Scalable search bar, imitating Huawei application market