Amazon Transcribe
Developer Guide

Using the Retry Client

The following is a sample application that uses the retry client to transcribe audio from either a file or your microphone. You can use this application to test the client, or you can use it as a starting point for your own applications.

To run the sample, do the following:

package com.amazonaws.wolverine.streaming.retryclient.example; public class SampleApp { private static final String endpoint = "endpoint"; private static final Region region = region; public static void main(String args[]) throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException, FileNotFoundException { /** * Create Transcribe streaming retry client using AWS credentials. */ TranscribeStreamingRetryClient client = new TranscribeStreamingRetryClient(getCredentials(), endpoint, region); StartStreamTranscriptionRequest request = StartStreamTranscriptionRequest.builder() .languageCode(LanguageCode.language.toString()) .mediaEncoding(encoding) .mediaSampleRateHertz(sample rate) .build(); /** * Start real-time speech recognition. The Transcribe streaming java client uses the Reactive-streams * interface. For reference on Reactive-streams: * */ 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 AudioStreamPublisher(InputStream inputStream) { this.inputStream = inputStream; } @Override public void subscribe(Subscriber<? super AudioStream> s) { if (currentSubscription == null) { this.currentSubscription = new SubscriptionImpl(s, inputStream); } else { this.currentSubscription.cancel(); this.currentSubscription = new SubscriptionImpl(s, inputStream); } s.onSubscribe(currentSubscription); } } }