@@ -418,6 +418,52 @@ RecognizeOptions options = new RecognizeOptions.Builder()
418418 // wait 20 seconds for the asynchronous response
419419 Thread.sleep (20000 );
420420```
421+ #### Microphone example
422+ Use your microphone to recognize audio for 30 seconds.
423+
424+ ```java
425+ SpeechToText service = new SpeechToText ();
426+ service.setUsernameAndPassword ("<username >", "<password >");
427+
428+ // Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
429+ int sampleRate = 16000 ;
430+ AudioFormat format = new AudioFormat (sampleRate, 16 , 1 , true , false );
431+ DataLine . Info info = new DataLine .Info (TargetDataLine . class, format);
432+
433+ if (!AudioSystem .isLineSupported (info )) {
434+ System . out. println(" Line not supported" );
435+ System . exit(0 );
436+ }
437+
438+ TargetDataLine line = (TargetDataLine ) AudioSystem . getLine(info);
439+ line.open (format );
440+ line.start ();
441+
442+ AudioInputStream audio = new AudioInputStream (line);
443+
444+ RecognizeOptions options = new RecognizeOptions .Builder ()
445+ .continuous(true )
446+ .interimResults(true )
447+ // .inactivityTimeout(5) // use this to stop listening when the speaker pauses, i.e. for 5s
448+ .contentType(HttpMediaType . AUDIO_RAW + " ; rate=" + sampleRate)
449+ .build();
450+
451+ service.recognizeUsingWebSocket (audio , options , new BaseRecognizeCallback () {
452+ @Override
453+ public void onTranscription(SpeechResults speechResults) {
454+ System . out. println(speechResults);
455+ }
456+ });
457+
458+ System.out.println ("Listening to your voice for the next 30s ...");
459+ Thread.sleep (30 * 1000 );
460+
461+ // closing the WebSockets underlying InputStream will close the WebSocket itself.
462+ line.stop ();
463+ line.close ();
464+
465+ System.out.println ("Fin .");
466+ ```
421467
422468### Text to Speech
423469Use the [Text to Speech ][text_to_speech] service to get the available voices to synthesize.
0 commit comments