如何在C#上使用Google TextToSpeech API生成音频文件? 我想为此项目使用Google API,能否提供一些示例或想法?
答案 0 :(得分:0)
使用Google TTS的第一步;在NuGet上将Google Cloud TextToSpeech添加到项目中。
第二步是https://cloud.google.com/的注册,因为在此链接的注册后google给出了一个json文件,您需要该json文件才能使用该API(json文件:“ XXXXXXXXXXXX.json”)。那是主要服务,您可以免费试用12个月。
private void GenerateVoice(string text, string type)
{
var credentials = GoogleCredential.FromFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "XXXXXXXXXXXX.json"));
var client = TextToSpeechClient.Create(credentials);
var response = client.SynthesizeSpeech
(
new SynthesisInput() //Text field
{
Text = text
},
new VoiceSelectionParams() //Language and Voice type field u can use normal or VaweNet sound types
{
LanguageCode = "tr-TR",
Name = type
},
new AudioConfig() //File type field
{
AudioEncoding = AudioEncoding.Mp3
}
);
var speechFile = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Ka_Ya.mp3"));
File.WriteAllBytes(speechFile, response.AudioContent);
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
if (txtDesc.Text.Trim().Length > 0)
{
GenerateVoice(txtDesc.Text, cmbType.Text);
}
}
希望本文对您有所帮助。
最诚挚的问候