Search Unity

Question Handling sound load error when getting it from local source

Discussion in 'Audio & Video' started by Jayme65, Feb 14, 2021.

  1. Jayme65

    Jayme65

    Joined:
    Dec 11, 2016
    Posts:
    94
    Hi,
    My app plays AudioClips from resource by default but offers the users the possibility to override them with local sounds.

    Code (CSharp):
    1. public class AudioManager : MonoBehaviour
    2. {
    3.     // User sounds
    4.     Dictionary<string, AudioClip> UserSoundFiles = new Dictionary<string, AudioClip>();
    5.     private string SoudFolderPath;
    6.  
    7.     void Start()
    8.     {
    9.         SoundFolderPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Application.dataPath), @"Sounds\"));
    10.         LoadUserSoundFiles();
    11.     }
    12.  
    13.     private void LoadUserSoundFiles()
    14.     {
    15.         UserSoundFiles.Add("Panel_Open.wav", null);
    16.         UserSoundFiles.Add("Panel_Close.wav", null);
    17.  
    18.         foreach (KeyValuePair<string, AudioClip> kvp in UserSoundFiles)
    19.         {
    20.             if (File.Exists(SoundFolderPath + kvp.Key))
    21.             {
    22.                 StartCoroutine(GetAudioClip(kvp.Key));
    23.             }
    24.         }
    25.     }
    26.  
    27.     IEnumerator GetAudioClip(string key)
    28.     {
    29.         using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(SoundFolderPath + key, AudioType.WAV))
    30.         {
    31.             yield return www.SendWebRequest();
    32.  
    33.             if (www.isHttpError || www.isNetworkError)
    34.             {
    35.             }
    36.             else
    37.             {
    38.                 AudioClip ac = DownloadHandlerAudioClip.GetContent(www);
    39.                 UserSoundFiles[key] = ac;
    40.             }
    41.         }
    42.     }
    43.  
    44.     public void playPanelOpen()
    45.     {
    46.         if (UserSoundFiles["Panel_Open.wav"] != null)
    47.         {
    48.             UIsounds.PlayOneShot(UserSoundFiles["Panel_Open.wav"]);
    49.             return;
    50.         }
    51.         UIsounds.PlayOneShot(Resource_Panel_Open);
    52.     }
    53.     public void playPanelClose()
    54.     {
    55.         if (UserSoundFiles["Panel_Close.wav"] != null)
    56.         {
    57.             UIsounds.PlayOneShot(UserSoundFiles["Panel_Close.wav"]);
    58.             return;
    59.         }
    60.         UIsounds.PlayOneShot(Resource_Panel_Close);
    61.     }
    62. }
    This is working fine. If a local sound file is present it is loaded then possibly played, otherwise the resource sound is played.
    ...but I wanted to know what would happen with a 'bad' .wav file, so I created a text file names 'Panel_Open.text' that I next renamed to 'Panel_Open.wav' to see what would happen.

    It appears that Unity is indeed rising this error
    Error: Cannot create FMOD::Sound instance for resource 0x0000000004BB7350: C:\Users\...\Desktop\Unity\Data\Sounds\Select.wav, (Unsupported file or audio format. )
    UnityEngine.Networking.DownloadHandlerAudioClip:GetContent(UnityWebRequest)


    ...but UserSoundFiles["Panel_Close.wav"] is not null anymore and there is an attempt to read it in 'playPanelOpen()'

    I hope it's clear so far ;) The question is: how could I catch the error from DownloadHandlerAudioClip.GetContent(www) and set the AudioClip to null in this case?

    Thank you!
     
    Last edited: Feb 14, 2021