演讲 Api 限制为 65 秒而不是 180 分钟

Speech Api limits to 65 seconds instead of 180 minutes

根据https://cloud.google.com/speech/quotas,异步请求持续 180 分钟。

但是当我使用这段代码时:

public async Task<object> StreamingMicRecognizeAsync(int seconds)
{
    streamingCall =  SpeechClient.Create().StreamingRecognize();
    await streamingCall.WriteAsync(
        new StreamingRecognizeRequest()
        {
            StreamingConfig = new StreamingRecognitionConfig()
            {
                Config = new RecognitionConfig()
                {
                    Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                    SampleRateHertz = 16000,
                    LanguageCode = "en-US" 
                },
                InterimResults = true,
                SingleUtterance = false
            }
        }
    );

    Task prinResp = Task.Run(async () =>
    {
        while (await streamingCall.ResponseStream.MoveNext(default(CancellationToken)))
        {
            foreach (var result in streamingCall.ResponseStream.Current.Results)
            {
                MessageBox.Show(result.Alternatives[0].Transcript.ToString());
            }
        }
    });

    // Read from the microphone and stream to API.
    object writeLock = new object();
    bool writeMore = true;
    waveIn = new NAudio.Wave.WaveInEvent();
    waveIn.DeviceNumber = 0;
    waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
    waveIn.DataAvailable +=
        (object sender, NAudio.Wave.WaveInEventArgs args) =>
        {
            lock (writeLock)
            {
                if (!writeMore || !isActive) return;
                try
                {
                    streamingCall.WriteAsync(
                        new StreamingRecognizeRequest()
                        {
                            AudioContent = Google.Protobuf.ByteString.CopyFrom(args.Buffer, 0, args.BytesRecorded)
                        }).Wait();
                }
                catch (Exception e) { my.message(e.Message); }
            }
        };

    waveIn.StartRecording();
    await Task.Delay(TimeSpan.FromSeconds(180*60));
    waveIn.StopRecording();
    lock (writeLock) writeMore = false;
    await streamingCall.WriteCompleteAsync();
    await prinResp; 
    return 0;
}

65 秒后 streamingCall.WriteAsync 显示 "Exceeded maximum allowed stream duration of 65 seconds." 错误。

如何获得 180 分钟? (即在 translate.google.com 上您可以使用 "speak" 功能超过一分钟,如何实现)?

如@CamiloTerevinto 所述,您需要为您的语音 API 的异步请求提供一个云存储 URI,以持续超过 1 分钟。 Translate.google.com 是一个 Google 工具,它们没有与 public API 相同的限制。我也相信翻译工具流式传输音频,将其上传到临时云存储,使用语音 API 转换为文本以便翻译。

您可以使用 Google.Cloud.Storage.V1 API 将流上传到 Google 云存储。然后,return URI 可与语音 API 异步请求一起使用,最多可处理 180 分钟的音频。

Google 有一些关于如何在不同场景中使用语音 API 的小 examples,包括 Google 存储 URI。我建议调查一下。

希望对您有所帮助