日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

如何通过 C# 将文本变为声音 ?

發(fā)布時間:2023/12/4 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何通过 C# 将文本变为声音 ? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

咨詢區(qū)

  • user2110292

我的項目有一個需求需要將可以將 文本 轉(zhuǎn)化為 聲音,請問大家是否有開源的 C# 庫 來解決這件事情?

回答區(qū)

  • HABJAN

最近 Google 發(fā)布了一個開源的 Google Cloud Text To Speech 包,.NET版本的github鏈接:https://github.com/jhabjan/Google.Cloud.TextToSpeech.V1

可參考下面的例子:

GoogleCredential?credentials?=GoogleCredential.FromFile(Path.Combine(Program.AppPath,?"jhabjan-test-47a56894d458.json"));TextToSpeechClient?client?=?TextToSpeechClient.Create(credentials);SynthesizeSpeechResponse?response?=?client.SynthesizeSpeech(new?SynthesisInput(){Text?=?"Google?Cloud?Text-to-Speech?enables?developers?to?synthesize?natural-sounding?speech?with?32?voices"},new?VoiceSelectionParams(){LanguageCode?=?"en-US",Name?=?"en-US-Wavenet-C"},new?AudioConfig(){AudioEncoding?=?AudioEncoding.Mp3} );string?speechFile?=?Path.Combine(Directory.GetCurrentDirectory(),?"sample.mp3");File.WriteAllBytes(speechFile,?response.AudioContent);
  • HABJAN

完全不需要使用任何開源框架,在 .NET 內(nèi)部提供的 System.Speech.Synthesis 類就可以幫你解決這個問題,引用下 System.speech.dll 命名空間即可。

using?System; using?System.Collections.Generic; using?System.ComponentModel; using?System.Data; using?System.Drawing; using?System.Linq; using?System.Text; using?System.Threading.Tasks; using?System.Windows.Forms; using?System.Speech.Synthesis;?//?first?import?this?packagenamespace?textToSpeech{public?partial?class?home?:?Form{public?string?s?=?"pran";?//?storing?string?(pran)?to?sprivate?void?home_Load(object?sender,?EventArgs?e){speech(s);?//?calling?the?function?with?a?string?argument}private?void?speech(string?args)?//?defining?the?function?which?will?accept?a?string?parameter{SpeechSynthesizer?synthesizer?=?new?SpeechSynthesizer();synthesizer.SelectVoiceByHints(VoiceGender.Male?,?VoiceAge.Adult);?//?to?change?VoiceGender?and?VoiceAge?check?out?those?links?belowsynthesizer.Volume?=?100;??//?(0?-?100)synthesizer.Rate?=?0;?????//?(-10?-?10)//?Synchronoussynthesizer.Speak("Now?I'm?speaking,?no?other?function'll?work");//?Asynchronoussynthesizer.SpeakAsync("Welcome"?+?args);?//?here?args?=?pran}???????}}

這里簡單提一下,最好用異步方式 SpeakAsync 替代同步的 Speak 方法,這樣的話,調(diào)用線程就不會被阻塞,提高程序的吞吐率。

點評區(qū)

這個功能好,不過建議大家了解下強大的 Google.Cloud.TextToSpeech。

總結(jié)

以上是生活随笔為你收集整理的如何通过 C# 将文本变为声音 ?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。