コード例 #1
0
    // Coroutine to upload recorded audio on server
    IEnumerator UploadAudioOnServer()
    {
        float[] samples = new float[AudioSrc.clip.samples * AudioSrc.clip.channels];
        AudioSrc.clip.GetData(samples, 0);

        var byteArray = new byte[samples.Length * 4];

        Buffer.BlockCopy(samples, 0, byteArray, 0, byteArray.Length);

        // Post recorded audio clips on server using UnityWebRequest
        using (UnityWebRequest WebRequest = UnityWebRequest.Post("https://staging.vertx.cloud/core/v1.0/resource/088c0839-d2d1-4808-87d4-a33ca223876e/audioClip.wav", ""))
        {
            WebRequest.SetRequestHeader("Content-Type", "application/octet-stream");
            WebRequest.uploadHandler             = new UploadHandlerRaw(byteArray);
            WebRequest.uploadHandler.contentType = "application/octet-stream";
            WebRequest.downloadHandler           = new DownloadHandlerBuffer();
            WebRequest.AddVertexAuth();
            yield return(WebRequest.SendWebRequest());

            Debug.Log("Audio file Uploaded");
        }
        yield return(null);

        isUploading = false;
    }
コード例 #2
0
    IEnumerator GetRecording()
    {
        isDownloading = true;
        using (UnityWebRequest www = UnityWebRequest.Get("https://staging.vertx.cloud/core/v1.0/resource/088c0839-d2d1-4808-87d4-a33ca223876e/audioClip.wav"))
        {
            www.SetRequestHeader("Content-Type", "application/octet-stream");
            www.AddVertexAuth();
            while (isUploading)
            {
                yield return(new WaitForSeconds(0.5f));
            }
            yield return(www.SendWebRequest());

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                byte[]  results       = www.downloadHandler.data;
                float[] downloadArray = new float[results.Length / 4];
                Buffer.BlockCopy(results, 0, downloadArray, 0, results.Length);

                AudioClip clip = AudioClip.Create("voice recording", downloadArray.Length, 1, AUDIO_SAMPLE_RATE, false);
                clip.SetData(downloadArray, 0);
                AudioSrc.clip = clip;

                Debug.Log("Audio file downloaded");
                //yield return new WaitForSeconds(7f);
            }
        }
        yield return(new WaitForSeconds(7f));

        isDownloading = false;
    }
コード例 #3
0
        private IEnumerator CreateHTTPRequest(string rootUri, string httpRequestPath)
        {
            UnityWebRequest www = new UnityWebRequest(Path.Combine(rootUri, httpRequestPath), "GET", new DownloadHandlerBuffer(), null);

            www.AddVertexAuth();

#if UNITY_2017_1_OR_NEWER
            yield return(www.SendWebRequest());
#else
            yield return(www.Send());
#endif
            if ((int)www.responseCode >= 400)
            {
                Debug.LogErrorFormat("{0} - {1}", www.responseCode, www.url);
                throw new Exception("Response code invalid");
            }

            if (www.downloadedBytes > int.MaxValue)
            {
                throw new Exception("Stream is larger than can be copied into byte array");
            }

            LoadedStream = new MemoryStream(www.downloadHandler.data, 0, www.downloadHandler.data.Length, true, true);
        }