/// <summary> /// InitializeSpeechRecognitionService is used to authenticate the client app /// with the Speech API Cognitive Services. A subscription key is passed to /// obtain a token, which is then used in the header of every APi call. /// </summary> private void InitializeSpeechRecognitionService() { // If you see an API key below, it's a trial key and will either expire soon or get invalidated. Please get your own key. // Get your own trial key to Bing Speech or the new Speech Service at https://azure.microsoft.com/try/cognitive-services // Create an Azure Cognitive Services Account: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account // DELETE THE NEXT THREE LINE ONCE YOU HAVE OBTAINED YOUR OWN SPEECH API KEY //Debug.Log("You forgot to initialize the sample with your own Speech API key. Visit https://azure.microsoft.com/try/cognitive-services to get started."); //Console.ReadLine(); //return; // END DELETE #if USENEWSPEECHSDK bool useClassicBingSpeechService = false; //string authenticationKey = @"INSERT-YOUR-NEW-SPEECH-API-KEY-HERE"; string authenticationKey = @"895664ef53e44b6fac574c3ecd6f3b75"; #else bool useClassicBingSpeechService = true; //string authenticationKey = @"INSERT-YOUR-BING-SPEECH-API-KEY-HERE"; string authenticationKey = @"4d5a1beefe364f8986d63a877ebd51d5"; #endif try { Debug.Log($"Instantiating Cognitive Services Speech Recognition Service client."); recoServiceClient = new SpeechRecognitionClient(useClassicBingSpeechService); // Make sure to match the region to the Azure region where you created the service. // Note the region is NOT used for the old Bing Speech service region = "westus"; auth = new CogSvcSocketAuthentication(); Task <string> authenticating = auth.Authenticate(authenticationKey, region, useClassicBingSpeechService); // Since the authentication process needs to run asynchronously, we run the code in a coroutine to // avoid blocking the main Unity thread. // Make sure you have successfully obtained a token before making any Speech Service calls. StartCoroutine(AuthenticateSpeechService(authenticating)); // Register an event to capture recognition events Debug.Log($"Registering Speech Recognition event handler."); recoServiceClient.OnMessageReceived += RecoServiceClient_OnMessageReceived; } catch (Exception ex) { string msg = String.Format("Error: Initialization failed. See error details below:{0}{1}{2}{3}", Environment.NewLine, ex.ToString(), Environment.NewLine, ex.Message); Debug.LogError(msg); UpdateUICanvasLabel(msg, FontStyle.Normal); } }
/// <summary> /// InitializeSpeechRecognitionService is used to authenticate the client app /// with the Speech API Cognitive Services. A subscription key is passed to /// obtain a token, which is then used in the header of every APi call. /// </summary> private void InitializeSpeechRecognitionService() { bool useClassicBingSpeechService = useNewCognitiveAPI == false; string authenticationKey = SpeechServiceAPIKey; Debug.Log("Auth key: " + authenticationKey); try { Debug.Log($"Instantiating Cognitive Services Speech Recognition Service client."); recoServiceClient = new SpeechRecognitionClient(useClassicBingSpeechService); // Make sure to match the region to the Azure region where you created the service. // Note the region is NOT used for the old Bing Speech service region = "northeurope"; auth = new CogSvcSocketAuthentication(); Task <string> authenticating = auth.Authenticate(authenticationKey, region, useClassicBingSpeechService); // Since the authentication process needs to run asynchronously, we run the code in a coroutine to // avoid blocking the main Unity thread. // Make sure you have successfully obtained a token before making any Speech Service calls. StartCoroutine(AuthenticateSpeechService(authenticating)); // Register an event to capture recognition events Debug.Log($"Registering Speech Recognition event handler."); recoServiceClient.OnMessageReceived += (r) => { Debug.Log("Message recevied" + r); Debug.Log(MessageReceived == null); MessageReceived?.Invoke(r); }; } catch (Exception ex) { string msg = String.Format("Error: Initialization failed. See error details below:{0}{1}{2}{3}", Environment.NewLine, ex.ToString(), Environment.NewLine, ex.Message); Debug.LogError(msg); OnError?.Invoke(msg); } }