使用 HTTP/2 重试客户端 - Amazon Transcribe
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 HTTP/2 重试客户端

以下是使用重试客户端转录来自文件或麦克风的音频的示例应用程序。您可以使用此应用程序来测试客户端,也可以将其用作您自己的应用程序的起点。

要运行示例,请执行以下操作:

  • 将重试客户端复制到您的工作区中。请参阅流式重试客户端代码

  • 将重试客户端接口复制到您的工作区中。实施接口,或者使用示例实施。请参阅流式重试客户端接口代码

  • 将示例应用程序复制到您的工作区。构建并运行应用程序。

public class StreamingRetryApp { private static final String endpoint = "endpoint"; private static final Region region = Region.US_EAST_1; private static final int sample_rate = 28800; private static final String encoding = " "; private static final String language = LanguageCode.EN_US.toString(); public static void main(String args[]) throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException, FileNotFoundException { /** * Create Amazon Transcribe streaming retry client. */ TranscribeStreamingRetryClient client = new TranscribeStreamingRetryClient(EnvironmentVariableCredentialsProvider.create() ,endpoint, region); StartStreamTranscriptionRequest request = StartStreamTranscriptionRequest.builder() .languageCode(language) .mediaEncoding(encoding) .mediaSampleRateHertz(sample_rate) .build(); /** * Start real-time speech recognition. The Amazon Transcribe streaming java client uses the Reactive-streams * interface. For reference on Reactive-streams: * https://github.com/reactive-streams/reactive-streams-jvm */ CompletableFuture<Void> result = client.startStreamTranscription( /** * Request parameters. Refer to API documentation for details. */ request, /** * Provide an input audio stream. * For input from a microphone, use getStreamFromMic(). * For input from a file, use getStreamFromFile(). */ new AudioStreamPublisher( new FileInputStream(new File("FileName"))), /** * Object that defines the behavior on how to handle the stream. */ new StreamTranscriptionBehaviorImpl()); /** * Synchronous wait for stream to close, and close client connection. */ result.get(); client.close(); } private static class AudioStreamPublisher implements Publisher<AudioStream> { private final InputStream inputStream; private static Subscription currentSubscription; private AudioStreamPublisher(InputStream inputStream) { this.inputStream = inputStream; } @Override public void subscribe(Subscriber<? super AudioStream> s) { if (this.currentSubscription == null) { this.currentSubscription = new TranscribeStreamingDemoApp.SubscriptionImpl(s, inputStream); } else { this.currentSubscription.cancel(); this.currentSubscription = new TranscribeStreamingDemoApp.SubscriptionImpl(s, inputStream); } s.onSubscribe(currentSubscription); } } }