这篇文章主要为大家详细介绍了C#实现文字转语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C#实现文字转语音的具体代码,供大家参考,具体内容如下
客户提出要求,将文字内容转为语音,因为内网环境,没办法采用联网,在线这种方式,灵机一动,能否写一个简单的例子呢,搜索相关资料还真行,话不多说,有图有真相
关键是,c#有现成的一个引用
右键点击项目 > 添加引用 > .Net > 找到System.Speech点击确定
控制台程序代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TxtToVoice
{
class Program
{
[STAThread] //默认线程模型是单线程单元 (STA) 模式
static void Main(string[] args)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//return;
OpenFileDialog open = new OpenFileDialog();
open.Title = "6K+36YCJ5oup5paH5pys"; //打开的文件选择对话框上的标题
open.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//设置文件类型
open.InitialDirectory = @"D:\project\";//默认打开目录
open.FilterIndex = 1;//设置默认文件类型显示顺序
open.RestoreDirectory = false;//是否记忆上次打开的目录
//open.Multiselect = true;//是否允许多选
string content=string.Empty;
if (open.ShowDialog() == DialogResult.OK)//按下确定选择的按钮
{
string[] filename = open.FileNames;//获取多个文件的路径及文件名并存入数组
MessageBox.Show(filename[0]);
// MessageBox.Show(filename[1]);
// MessageBox.Show(open.FileName); //获取路径及文件名
// MessageBox.Show(open.SafeFileName);//获取文件名
content = ReadFile(filename[0]);
}
//-----------------------------------读出文件内容---------------------------------
SpeechSynthesizer voice = new SpeechSynthesizer(); //创建语音实例
voice.Rate = -1; //设置语速,[-10,10]
voice.Volume = 100; //设置音量,[0,100]
//voice.SpeakAsync("Hellow Word"); //播放指定的字符串,这是异步朗读
//下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用
voice.SpeakAsyncCancelAll(); //取消朗读
voice.Speak(content); //同步朗读
voice.Pause(); //暂停朗读
voice.Resume(); //继续朗读
voice.Dispose(); //释放所有语音资源
}
/// <summary>
/// 读取文件,返回相应字符串
/// </summary>
/// <param name="fileName">文件路径</param>
/// <returns>返回文件内容</returns>
private static string ReadFile(string fileName)
{
StringBuilder str = new StringBuilder();
using (FileStream fs = File.OpenRead(fileName))
{
long left = fs.Length;
int maxLength = 100;//每次读取的最大长度
int start = 0;//起始位置
int num = 0;//已读取长度
while (left > 0)
{
byte[] buffer = new byte[maxLength];//缓存读取结果
char[] cbuffer = new char[maxLength];
fs.Position = start;//读取开始的位置
num = 0;
if (left < maxLength)
{
num = fs.Read(buffer, 0, Convert.ToInt32(left));
}
else
{
num = fs.Read(buffer, 0, maxLength);
}
if (num == 0)
{
break;
}
start += num;
left -= num;
str = str.Append(Encoding.UTF8.GetString(buffer));
}
}
return str.ToString();
}
}
}
窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TxtToVoiceForm
{
public partial class Form2 : Form
{
private SpeechSynthesizer speech;
/// <summary>
/// 音量
/// </summary>
private int value = 100;
/// <summary>
/// 语速
/// </summary>
private int rate;
public Form2()
{
InitializeComponent();
ReadlocalFile();
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
rate = Int32.Parse(comboBox1.Text);
}
//private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
//{
// this.ReadlocalFile();
/
沃梦达教程
本文标题为:C#实现文字转语音功能
猜你喜欢
- Unity Shader实现模糊效果 2023-04-27
- Unity3D实现渐变颜色效果 2023-01-16
- c# 模拟线性回归的示例 2023-03-14
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- user32.dll 函数说明小结 2022-12-26
- .NET CORE DI 依赖注入 2023-09-27
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Oracle中for循环的使用方法 2023-07-04
- 如何使用C# 捕获进程输出 2023-03-10
- 在C# 8中如何使用默认接口方法详解 2023-03-29