Hi Nextclass,
Greetings & Welcome to the Microsoft Q&A forum! Thank you for sharing your query.
I Tried to repro the code in my environment it works fine.
Here the code
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class SpeechRecognitionExample {
// This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
private static String speechKey = System.getenv("SPEECH_KEY");
private static String speechRegion = System.getenv("SPEECH_REGION");
public static void main(String[] args) throws InterruptedException, ExecutionException {
SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);
speechConfig.setSpeechRecognitionLanguage("en-US");
recognizeFromMicrophone(speechConfig);
}
public static void recognizeFromMicrophone(SpeechConfig speechConfig) throws InterruptedException, ExecutionException {
AudioConfig audioConfig = AudioConfig.fromWavFileInput("paste the wavfile location");
SpeechRecognizer speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);
System.out.println("Speak into your microphone.");
Future<SpeechRecognitionResult> task = speechRecognizer.recognizeOnceAsync();
SpeechRecognitionResult speechRecognitionResult = task.get();
if (speechRecognitionResult.getReason() == ResultReason.RecognizedSpeech) {
System.out.println("RECOGNIZED: Text=" + speechRecognitionResult.getText());
}
else if (speechRecognitionResult.getReason() == ResultReason.NoMatch) {
System.out.println("NOMATCH: Speech could not be recognized.");
}
else if (speechRecognitionResult.getReason() == ResultReason.Canceled) {
CancellationDetails cancellation = CancellationDetails.fromResult(speechRecognitionResult);
System.out.println("CANCELED: Reason=" + cancellation.getReason());
if (cancellation.getReason() == CancellationReason.Error) {
System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode());
System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails());
System.out.println("CANCELED: Did you set the speech resource key and region values?");
}
}
System.exit(0);
}
}
To set the environment variables for your Speech resource key and region, open a console window, and follow the instructions for your operating system and development environment.
- To set the
SPEECH_KEY
environment variable, replace your-key with one of the keys for your resource. - To set the
SPEECH_REGION
environment variable, replace your-region with one of the regions for your resource. -
setx SPEECH_KEY your-key setx SPEECH_REGION your-region
your-key
- you have pasted your speech services keys
your-region
- you have pasted your speech service region
kindly please refer this document https://zcusa.951200.xyz/en-us/azure/ai-services/speech-service/get-started-speech-to-text?tabs=windows%2Cterminal&pivots=programming-language-java
I hope this helps you. Thank you.