Advertisement
Guest User

UnityWebRequest instead of UniWeb

a guest
Aug 30th, 2016
2,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1.             // Construct JSON request body.
  2.             JSONObject requestJSON = new JSONObject();
  3.             JSONObject requestConfig = new JSONObject();
  4.             requestConfig.AddField(Constants.GoogleRequestJSONConfigEncodingFieldKey, "FLAC");
  5.             requestConfig.AddField(Constants.GoogleRequestJSONConfigSampleRateFieldKey, "16000");
  6.             requestConfig.AddField(Constants.GoogleRequestJSONConfigLanguageCodeFieldKey, "ru-RU");
  7.             JSONObject requestAudio = new JSONObject();
  8.             requestAudio.AddField(Constants.GoogleRequestJSONAudioContentFieldKey, Convert.ToBase64String(File.ReadAllBytes(flacAudioFilePath)));
  9.             requestJSON.AddField(Constants.GoogleRequestJSONConfigFieldKey, requestConfig);
  10.             requestJSON.AddField(Constants.GoogleRequestJSONAudioFieldKey, requestAudio);
  11.  
  12.             string url = Constants.GoogleNonStreamingSpeechToTextURL +
  13.                          "?" + Constants.GoogleAPIKeyParameterName + "=" + m_APIKey;
  14.  
  15.             UnityWebRequest www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
  16.  
  17.             byte[] bytes = Encoding.UTF8.GetBytes(requestJSON.ToString());
  18.  
  19.             UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
  20.             uH.contentType = "application/json";
  21.             www.uploadHandler = uH;
  22.  
  23.             www.downloadHandler = new DownloadHandlerBuffer();
  24.  
  25.             www.SetRequestHeader("Content-Type", "application/json");
  26.  
  27.             SmartLogger.Log(DebugFlags.GoogleNonStreamingSpeechToText, "sent request");
  28.  
  29.             yield return www.Send();
  30.  
  31.             while (!www.isDone)
  32.             {
  33.                 yield return null;
  34.             }
  35.  
  36.             if (www.isError)
  37.             {
  38.                 SmartLogger.Log(DebugFlags.GoogleNonStreamingSpeechToText, www.error);
  39.             }
  40.             else
  41.             {
  42.                 SmartLogger.Log(DebugFlags.GoogleNonStreamingSpeechToText, "Form upload complete!");
  43.             }
  44.  
  45.             // Grab the response JSON once the request is done and parse it.
  46.             var responseJSON = new JSONObject(www.downloadHandler.text, int.MaxValue);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement