0
\$\begingroup\$
    private void Upload(){

    string apiURL = Globals.GoogleSpeechToTextURL + Globals.GoogleSpeechToTextApiKey;
                string Response;

                Debug.Log("Uploading " + filePath);

                var fileInfo = new System.IO.FileInfo(filePath);
                Debug.Log("Size: "+fileInfo.Length);
                gtext.text = fileInfo.Length + " ";
                Response = HttpUploadFile(apiURL, filePath, "file", "audio/wav; rate=44100");
                var list = new List<Event>();

                JObject obj = JObject.Parse(Response);
                //return event array
                var token = (JArray)obj.SelectToken("results");

                if (token != null)
                {

                    foreach (var item in token)
                    {
                        string json = JsonConvert.SerializeObject(item.SelectToken("alternatives")[0]);
                        list.Add(JsonConvert.DeserializeObject<Event>(json));
                    }

                    Debug.Log("Response String: " + list[0].transcript);
              }
        }

          public string HttpUploadFile(string url, string file, string paramName, string contentType)
            {

                System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
                Debug.Log(string.Format("Uploading {0} to {1}", file, url));

                Byte[] bytes = File.ReadAllBytes(file);
                String file64 = Convert.ToBase64String(bytes,
                                                 Base64FormattingOptions.None);

                try
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Method = "POST";
                    httpWebRequest.Proxy = null;

                    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {

                        string json = "{ \"config\": { \"languageCode\" : \"en-US\" }, \"audio\" : { \"content\" : \"" + file64 + "\"}}";

                        // Debug.Log(json);
                        streamWriter.Write(json);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    //  Debug.Log(httpResponse);

                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        return result;
                    }

                }
                catch (WebException ex)
                {
                    var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
                    // Debug.Log(resp);

                }
                return "empty";

            }

This is the code I am using to upload an audio file. When HttpUploadFile gets called, the app pauses and shows a black screen for a second and then continues. I tried calling Upload function through coroutine, and also tried this https://stackoverflow.com/questions/41330771/use-unity-api-from-another-thread-or-call-a-function-in-the-main-thread. But still it pauses while uploading. The file size is around 200 KB

What else can I try to avoid this problem?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.