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. Dismiss Notice

Question MP3 Audio to WebGL?

Discussion in 'WebGL' started by GleetchDev, Jun 13, 2023.

  1. GleetchDev

    GleetchDev

    Joined:
    Mar 18, 2021
    Posts:
    5
    My webapp should be hosted on a website and it should receive an MP3 audio in runtime to then reproduce through the AudioSource.

    Due to webgl limitation I cannot pass directly the mp3 audio to the unity app so the backend of the webstite converts the generated audio first into an array of bytes and then into a string to be able to be attached to a SendMessage call.

    The problem is when I get this data and reverse the process, string to array of byte is fine but I haven't found any way to convert that []byte back to an mp3/audioclip.

    Is somebody able to help?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
  3. GleetchDev

    GleetchDev

    Joined:
    Mar 18, 2021
    Posts:
    5
    Keep in mind that WebGL has significanrt limitation especially in the area of saving/writing files locally so most of the solutions I found online are not working.


    Code (CSharp):
    1.  public void Test()
    2.     {
    3.         ReceiveAudioString(testString);
    4.        // ReceiveAudioString_Old(testString);
    5.     }
    6.  
    7.     private void ReceiveAudioString(string audioString)
    8.     {
    9.         if (string.IsNullOrEmpty(audioString))
    10.         {
    11.             Debug.Log("String is NULL or EMPTY");
    12.             return;
    13.         }
    14.  
    15.         byte[] byteArray = System.Convert.FromBase64String(audioString);
    16.  
    17.         StartCoroutine(LoadAudioClip(byteArray));
    18.     }
    19.  
    20.     private IEnumerator LoadAudioClip(byte[] audioBytes)
    21.     {
    22.         string tempFile = Application.persistentDataPath + "/bytes.mp3";
    23.         System.IO.File.WriteAllBytes(tempFile, audioBytes);
    24.  
    25.  
    26.         string path = "";
    27.  
    28. #if UNITY_EDITOR
    29.         Debug.Log("EDITOR");
    30.  
    31.         path = "file://";
    32. #endif
    33.  
    34. #if  UNITY_WEBGL && !UNITY_EDITOR
    35.         Debug.Log("WEBGL");
    36.  
    37.         path = "http";
    38. #endif
    39.  
    40.         UnityWebRequest audioRequest = UnityWebRequestMultimedia.GetAudioClip(path + tempFile, AudioType.MPEG);
    41.  
    42.  
    43.         yield return audioRequest.SendWebRequest();
    44.  
    45.         if (audioRequest.result != UnityWebRequest.Result.Success)
    46.         {
    47.             Debug.LogError(audioRequest.error);
    48.             yield break;
    49.         }
    50.  
    51.         AudioClip newClip = DownloadHandlerAudioClip.GetContent(audioRequest);
    52.         newClip.name = "TextToSpeech";
    53.  
    54.         voiceTest.audioSource.clip = newClip;
    55.         voiceTest.audioSource.Play();
    56.     }
    57.  
    These are all the methods I use to get the MP3 from the website backend, first I get a string (which in this case is the same so I can test it locally on editor), then I turn it into an array of bytes and then I should get an AudioClip.

    This system works like a charm on editor but it fails on WebGL probably where I do insert the path for UnityWebRequestMultimedia but I'm not sure since documentation about this topic seems pretty rare.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Hmmm how big is that MP3? You are extremely limited in how much you can save with the File API in WebGL. 10 MB is the maximum (all files, not single file).

    After writing the file, try reading it back in again and compare the two byte arrays. You can use the built-in MD5 checksum API but I'm not sure if Cryptography is available on WebGL:
    https://stackoverflow.com/questions/11454004/calculate-a-md5-hash-from-a-string
     
  5. GleetchDev

    GleetchDev

    Joined:
    Mar 18, 2021
    Posts:
    5
    File size shouldn't be an issue since I've tried to save the generated MP3 on editor and its weight its 82,4 KB.
    Consider that everytime a new audio gets generated I can dispose or overwrite the old one to save storage.

    I don't know any of the APIs you mentioned tho, can you explain how they works or link to a documentation?
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I meant System.IO.File ;)