Azure 文本到语音 API 音频输出未播放

问题描述 投票:0回答:1

我是 Azure 新手,也是一般编程新手。

背景

我正在构建一个 Google Sheet 应用程序脚本,它将 Sheet2!B1 的内容发送到 Azure Speech API,并将生成的语音输出到 Sheet2!B2。但是,尽管使用

X-Microsoft-OutputFormat
作为
riff-48khz-16bit-mono-pcm
,输出音频文件仍无法播放。我尝试根据我在这里看到的
答案将输出更改为
audio-24khz-160kbitrate-mono-mp3,但它也不起作用。我也尝试过将文件类型更改为 .wav,但这也不起作用。

这是代码。你能建议我应该做什么吗?此外,任何关于创建批量 api 请求的提示都值得赞赏。谢谢你。

function sendTextToAzureTTS() { // Azure Text to Speech endpoint and API key var endpoint = "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1"; var apiKey = "hidden"; // Get the plain text from Sheet2 B1 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2"); var text = sheet.getRange("B1").getValue(); // Create the request payload var requestData = { method: "POST", contentType: "application/ssml+xml", headers: { "Ocp-Apim-Subscription-Key": apiKey, "X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3", // Use the supported format }, payload: '<speak version="1.0" xml:lang="en-US"><voice xml:lang="en-US" xml:gender="Female" name="en-US-EmmaNeural">' + text + '</voice></speak>', }; // Make the POST request to Azure Text to Speech var response = UrlFetchApp.fetch(endpoint, requestData); // Check if the response was successful if (response.getResponseCode() == 200) { // Get the audio data from the response var audioData = response.getBlob().getBytes(); // Write the audio data to a file in Google Drive with the correct MIME type var audioFile = DriveApp.createFile('otuput_audio.mp3', audioData, "audio/mp3"); // Get the URL of the created audio file var audioUrl = audioFile.getUrl(); // Write the audio URL to Sheet2 B2 sheet.getRange("B2").setValue(audioUrl); } else { Logger.log("Error: " + response.getResponseCode() + " - " + response.getContentText()); } }
我尝试创建天蓝色语音发布请求并通过 Google Sheet 应用程序脚本取回音频文件。但是,音频文件未播放。我期望一个可以工作的音频文件,但该音频文件没有播放。

azure azure-web-app-service text-to-speech azure-api-management azure-speech
1个回答
0
投票
我在 Azure 门户中创建了语音服务。

enter image description here

我创建了一个 Google 电子表格并将其命名为

TextToSpeech

。将触发器添加到以下电子表格中。

代码:

function sendTextToAzureTTS() { // Azure Text to Speech region and access key var region = "eastus"; // Replace with your Azure region var apiKey = "your-api-key"; // Replace with your Azure access key var endpoint = "your speech servive endpoint"; var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TextToSpeech"); var text = sheet.getRange("B1").getValue(); var requestData = { method: "POST", contentType: "application/ssml+xml", headers: { "Ocp-Apim-Subscription-Key": apiKey, "X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3", }, payload: '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US"><voice name="en-US-AriaNeural">' + text + '</voice></speak>', }; var response = UrlFetchApp.fetch(endpoint, requestData); if (response.getResponseCode() == 200) { var audioData = response.getBlob().getBytes(); var audioFile = DriveApp.createFile('output_audio.mp3', audioData, "audio/mpeg"); var audioUrl = audioFile.getUrl(); // Write the audio URL to the same sheet in cell B2 sheet.getRange("B2").setValue(audioUrl); } else { Logger.log("Error: " + response.getResponseCode() + " - " + response.getContentText()); } }

    上面的代码放置在脚本编辑器中,我将我的语音服务 API 密钥添加到上面的代码中。
  • 我在单元格 B1 中给出了一段文本,我想将其转换为音频。
  • 当我运行脚本时,它执行成功。
  • 然后我就可以在单元格 B2 中获取音频文件 URL。

enter image description here

输出: enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.