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 Handle "Cannot create FMOD" on UnityWebRequestMultimedia.GetAudioClip

Discussion in 'Audio & Video' started by marck_ozz, Jul 12, 2021.

  1. marck_ozz

    marck_ozz

    Joined:
    Nov 30, 2018
    Posts:
    107
    Hello!

    I making a game where the users can upload audio files from their local disks, so, in order to check that those files are actual audio files I'm checking for a valid audio codec using "MediainfoLib" and for testing it I'm using "wrong" or "corrupted" files (e.g literally changing the extension like "file.txt" for "file.mp3") because I'm looking for ".mp3", ".wav". ".ogg" and all of this basically works fine.

    The problem is that with the "corrupted files" the "UnityWebRequestMultimedia.GetAudioClip" gets
    Error: Cannot create FMOD::Sound instance for clip "" (FMOD error: Unsupported file or audio format. )
    which seem logic since those are not audio files but my question is:

    - How can I handle this error or what could be the proper way to manage it?


    This is my code:

    Code (CSharp):
    1. IEnumerator GetBytes(string path, string fileName)
    2.     {
    3.         Debug.Log("Writing files");
    4.         string ext;
    5.         ext = Path.GetExtension(fileName);
    6.      
    7.         UnityWebRequest.ClearCookieCache();
    8.  
    9.         UnityWebRequest www;
    10.  
    11. HashSet<string> set3 = new HashSet<string>() { ".mp3", ".wav", ".ogg" };
    12.         if (set3.Contains(ext))
    13.         {
    14.             using (www = UnityWebRequestMultimedia.GetAudioClip("file://" + path, AudioType.UNKNOWN))
    15.             {
    16.                 yield return www.SendWebRequest();
    17.                 if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
    18.                 {
    19.                     Debug.LogError(www.error);
    20.                     yield break;
    21.                 }
    22.  
    23.                 if (DownloadHandlerAudioClip.GetContent(www).length <= 60)
    24.                 {
    25.                     Debug.Log("Track Length = " + DownloadHandlerAudioClip.GetContent(www).length);
    26.                     byte[] results;
    27.                     results = www.downloadHandler.data;
    28.                     GetInfoFomMemory(results, ext, www);my method to use "MediainfoLib, this works fine
    29.                    yield return new WaitUntil(() => codecMsgDone);
    30.  
    31.                    if (mediaInfo.codecOK)
    32.                    {
    33. #if UNITY_EDITOR || UNITY_STANDALONE_WIN
    34.                                    System.IO.File.WriteAllBytes(path_, results);
    35.                                    resultOK = true;
    36.                                    resultTrackOK = true;
    37. #endif
    38.                        clipName = fileName;
    39.                        trackSource.clip = DownloadHandlerAudioClip.GetContent(www);
    40.                        www.Dispose();
    41.                    }
    42.                }
    43.                else
    44.                {
    45.                    Debug.LogError("Audio track too long");
    46.                    resultOK = false;
    47.  
    48.                    if (File.Exists(path))
    49.                        File.Delete(path);
    50.                }
    51.            }
    52.        }
    53.    }
    The error seems to be "ignored" since the rest of the code are executed (which is weird) and the build works as should be, uploading only actual audio files and triggering a message telling the users if they are using invalid audio files, but I don't think is a good idea just to let erros just happen like that.

    I have read this Issue Tracker and this thread but I don't think that is quite the same issue. Could this be a bug?

    Thanks in advance

    Edit-
    Using Unity 2020.3.3f1
    Testing on Windows Standalone
     
  2. fredholmsimon

    fredholmsimon

    Joined:
    Sep 5, 2020
    Posts:
    92
    I'm having the same issue.
     
  3. yotamano

    yotamano

    Joined:
    Dec 26, 2021
    Posts:
    1
    did you manage to solve it?
     
  4. Rafael-cmk

    Rafael-cmk

    Joined:
    Jun 28, 2016
    Posts:
    55
    I have the same feature on my Android game, and this error is crashing my app. I also haven't found solution for it yet.
     
  5. Rafael-cmk

    Rafael-cmk

    Joined:
    Jun 28, 2016
    Posts:
    55
    I just found a workaround for this, which is essentially to check the mp3 file yourself before loading it.
    based of: https://stackoverflow.com/questions...that-a-particular-file-is-in-fact-an-mp3-file

    Code (CSharp):
    1. public bool IsMp3File(string filePath)
    2.     {
    3.         byte[] mp3Header = { 0xFF, 0xFB }; // Typical MP3 frame sync bits.
    4.         byte[] id3Header = { 0x49, 0x44, 0x33 }; // 'ID3' in ASCII.
    5.  
    6.         byte[] bytes = new byte[3]; // Read the first three bytes of the file.
    7.  
    8.         using (var file = File.OpenRead(filePath))
    9.         {
    10.             if (file.Length < 3)
    11.             {
    12.                 return false;
    13.             }
    14.  
    15.             file.Read(bytes, 0, 3);
    16.         }
    17.  
    18.         // Return true if we found an MP3 frame header or an ID3v2 tag.
    19.         return bytes.SequenceEqual(mp3Header) || bytes.SequenceEqual(id3Header);
    20.     }