使用System.Speech实现语音合成

因为项目需要,需要将文字转为语音。上网搜索了一下,发现微软有这方面的库 System.Speech ,于是拿来直接使用。不过在使用中遇到一些问题,现在总结一下。

使用方法

一、引用System.Speech

 

 

二、几行代码

using System;
using System.Speech.Synthesis;

namespace Util
{
    public class SpeechHelper
    {
        /// <summary>
        /// 语音合成方法
        /// </summary>
        /// <param name="ReadInfo">转写的文字内容</param>
        /// <param name="FilePath">保存的文件路径</param>
        public static void TTSRun(String ReadInfo,string FilePath)
        { 
            SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.SetOutputToDefaultAudioDevice();
            synth.SetOutputToWaveFile(FilePath);
            synth.Speak(ReadInfo);
            synth.SetOutputToNull();
        }
    }
}

 

问题总结:

问题一:选择合适的版本

System.Speech 目前最新版本是7.0.0。兼容.net6 和 .net7 。而我当前项目是 .net5已不受支持,所以选择版本的时候,选择了较老的版本 5.0.0

 

问题二:开发环境的系统需要下载对应的 SDK包

下载地址:https://developer.microsoft.com/zh-cn/windows/downloads/sdk-archive/

根据自己的系统,下载对应的sdk。不然可能会报“当前设备不支持”的错误

 

posted @ 2023-02-01 11:09  leoxuan  阅读(817)  评论(0编辑  收藏  举报