Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Problem in call Microsoft RIST API by UnityWebRequest

Discussion in 'Editor & General Support' started by DataMeshDev, Apr 11, 2018.

  1. DataMeshDev

    DataMeshDev

    Joined:
    Nov 16, 2017
    Posts:
    5
    I want to call microsoft RIST API ( text to speech) in Unity.
    When I use WWW, it works fine, I can get the speech WAV file immediately. But when i change to UnityWebRequest in same properties, it always no response until timeout. (The token has generated successfully.)
    I don't know why. Could any guys can tell me where is the mistake I make?
    Unity version: 2017.3.1

    it is my code by WWW:
    Code (CSharp):
    1.         string text = "<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)'>Today is a good day to die!</voice></speak>";
    2.         byte[] data = System.Text.Encoding.UTF8.GetBytes(text);
    3.  
    4.         Dictionary<string, string> headers = new Dictionary<string, string>();
    5.         headers.Add("X-Microsoft-OutputFormat", "riff-8khz-8bit-mono-mulaw");
    6.         headers.Add("Content-Type", "application/ssml+xml");
    7.         headers.Add("Authorization", "Bearer " + token);
    8.  
    9.         WWW www = new WWW(url, data, headers);
    10.  
    11.         yield return www;
    12.  
    13.         if (www.error != null)
    14.         {
    15.             Debug.LogError(www.error);
    16.         }
    17.         else
    18.         {
    19.             Debug.Log(www.bytes.Length);
    20.             File.WriteAllBytes(Application.streamingAssetsPath + "/receive.wav", www.bytes);
    21.         }
    and, it is my code by UnityWebRequest:
    Code (CSharp):
    1.         string text = "<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)'>Today is a good day to die!</voice></speak>";
    2.         byte[] data = System.Text.Encoding.UTF8.GetBytes(text);
    3.  
    4.         UnityWebRequest req = new UnityWebRequest(url);
    5.         req.method = UnityWebRequest.kHttpVerbPOST;
    6.         req.SetRequestHeader("X-Microsoft-OutputFormat", "riff-8khz-8bit-mono-mulaw");
    7.         req.SetRequestHeader("Content-Type", "application/ssml+xml");
    8.         req.SetRequestHeader("Authorization", "Bearer " + token);
    9.  
    10.         req.uploadHandler = new UploadHandlerRaw(data);
    11.         req.downloadHandler = new DownloadHandlerBuffer();
    12.  
    13.         req.timeout = 10;
    14.  
    15.         yield return req.SendWebRequest();
    16.  
    17.         if (req.error != null)
    18.         {
    19.             Debug.LogError(req.error + " [code=" + req.responseCode + "]");
    20.         }
    21.         else
    22.         {
    23.             if (req.responseCode == 200)
    24.             {
    25.                 Debug.Log("Success!");
    26.                 File.WriteAllBytes(Application.streamingAssetsPath + "/receive.wav", req.downloadHandler.data);
    27.             }
    28.             else
    29.             {
    30.                 Debug.LogWarning("Parse Failed! code=" + req.responseCode);
    31.             }
    32.         }
     
    Last edited: Apr 11, 2018
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,635
    The 10 second timeout looks way too low.
    Don't know which Unity version you are on, but try setting chunkedTransfer property to false.
     
  3. DataMeshDev

    DataMeshDev

    Joined:
    Nov 16, 2017
    Posts:
    5
    Unity version is 2017.3.1f1
    if I use WWW, it will almost return result immediate, so 10 second is long enough I think.
    and, I have tried 30 second timeout, the result is same...
     
  4. MattuWalk

    MattuWalk

    Joined:
    Apr 10, 2018
    Posts:
    2