stt AudioConfig.fromWavFileInput The program is not running anymore

Nextclass 0 Reputation points
2025-01-09T10:00:42.6366667+00:00
log.info("azureAsrService sn={} filepath={}",sn,filepath);
AudioConfig audioConfig = AudioConfig.fromWavFileInput(filepath);
log.info("init audioConfig");
SpeechRecognizer recognizer = new SpeechRecognizer(getSpeechConfig(sn), audioConfig);
log.info("init recognizer");
try {
    Future
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,856 questions
{count} votes

1 answer

Sort by: Most helpful
  1. kothapally Snigdha 850 Reputation points Microsoft Vendor
    2025-01-09T17:37:57.15+00:00

    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.
    • User's image
        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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.