|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:aws_common/aws_common.dart'; |
| 5 | +import 'package:aws_transcribe_streaming/aws_transcribe_streaming.dart'; |
| 6 | + |
| 7 | +void main() async { |
| 8 | + // Create a client. |
| 9 | + final transcribeStreamingClient = TranscribeStreamingClient( |
| 10 | + region: 'eu-central-1', |
| 11 | + // Provide credentials with `transcribe:StartStreamTranscription` permission |
| 12 | + credentialsProvider: StaticCredentialsProvider(AWSCredentials( |
| 13 | + 'ASIAIOEXAMPLEEXAMPLE', // accessKeyId |
| 14 | + 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', // secretAccessKey |
| 15 | + 'AQoDYXdzEJr...', // sessionToken |
| 16 | + DateTime.now().add(const Duration(hours: 1)), // expiration |
| 17 | + )), |
| 18 | + ); |
| 19 | + |
| 20 | + late final StartStreamTranscriptionResponse response; |
| 21 | + late final Stream<TranscriptEvent> transcriptEventStream; |
| 22 | + late final StreamSink<Uint8List> audioStreamSink; |
| 23 | + StreamSubscription<Uint8List>? audioStreamSubscription; |
| 24 | + |
| 25 | + try { |
| 26 | + // Start a stream transcription. |
| 27 | + (response, audioStreamSink, transcriptEventStream) = |
| 28 | + await transcribeStreamingClient.startStreamTranscription( |
| 29 | + const StartStreamTranscriptionRequest( |
| 30 | + languageCode: LanguageCode.enUs, |
| 31 | + mediaSampleRateHertz: 48000, |
| 32 | + mediaEncoding: MediaEncoding.pcm, |
| 33 | + ), |
| 34 | + ); |
| 35 | + } on TranscribeStreamingException catch (e) { |
| 36 | + print('Error starting transcription: $e'); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + print('Session ID: ${response.sessionId}'); |
| 41 | + |
| 42 | + final transcriptionCompleter = Completer<void>(); |
| 43 | + |
| 44 | + // Listen to transcript events. |
| 45 | + // final transcriptSubscription = |
| 46 | + // transcriptEventStream.listen((TranscriptEvent event) => print(event)); |
| 47 | + // or use a custom strategy to decode transcript events. |
| 48 | + transcriptEventStream |
| 49 | + .transform( |
| 50 | + const TranscriptEventStreamDecoder(PlainTextTranscriptionStrategy())) |
| 51 | + .listen( |
| 52 | + (String message) { |
| 53 | + print('Transcription: $message'); |
| 54 | + }, |
| 55 | + onError: (Object error, StackTrace stackTrace) async { |
| 56 | + print('Transcription error: $error'); |
| 57 | + await audioStreamSubscription?.cancel(); |
| 58 | + await audioStreamSink.close(); |
| 59 | + }, |
| 60 | + onDone: () async { |
| 61 | + print('Transcription done'); |
| 62 | + await audioStreamSubscription?.cancel(); |
| 63 | + transcriptionCompleter.complete(); |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + // Instead use a real stream of audio data from the microphone |
| 68 | + // in PCM signed 16-bit little-endian audio format with 48kHz sample rate. |
| 69 | + final audioStream = Stream<Uint8List>.periodic( |
| 70 | + const Duration(milliseconds: 200), (count) => Uint8List(19200)).take(25); |
| 71 | + |
| 72 | + // Send audio data to the audio stream sink. |
| 73 | + audioStreamSubscription = audioStream.listen( |
| 74 | + audioStreamSink.add, |
| 75 | + onDone: audioStreamSink.close, |
| 76 | + ); |
| 77 | + |
| 78 | + await transcriptionCompleter.future; |
| 79 | + |
| 80 | + print('Finished'); |
| 81 | +} |
0 commit comments