- AWS Code Sample demonstrates how to use the AWS Transcribe service to transcribe an audio input from the microphone.

/* Copyright, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.amazonaws.transcribe; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.TargetDataLine; import javax.sound.sampled.AudioInputStream; import; import; import; import; import; import; import; public class BidirectionalStreaming { public static void main(String[] args) throws Exception { TranscribeStreamingAsyncClient client = TranscribeStreamingAsyncClient.builder().credentialsProvider(ProfileCredentialsProvider.create()).build(); StartStreamTranscriptionRequest request = StartStreamTranscriptionRequest.builder() .mediaEncoding(MediaEncoding.PCM) .languageCode(LanguageCode.EN_US) .mediaSampleRateHertz(16_000).build(); TargetDataLine mic = Microphone.get(); mic.start(); AudioStreamPublisher publisher = new AudioStreamPublisher(new AudioInputStream(mic)); StartStreamTranscriptionResponseHandler response = StartStreamTranscriptionResponseHandler.builder().subscriber(e -> { TranscriptEvent event = (TranscriptEvent) e; event.transcript().results().forEach(r -> r.alternatives().forEach(a -> System.out.println(a.transcript()))); }).build(); client.startStreamTranscription(request, publisher, response).join(); } public static TargetDataLine get() throws Exception { AudioFormat format = new AudioFormat(16000, 16, 1, true, false); DataLine.Info datalineInfo = new DataLine.Info(TargetDataLine.class, format); TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(datalineInfo);; return dataLine; } }

Sample Details

Service: transcribe

Last tested: 2019-04-18

Author: AWS

Type: full-example