Question by
laurienash · Mar 19, 2021 at 01:05 PM ·
c#audioexceptionstreamingassets
Playing audio from streaming assets - how do I stop error being thrown if file isn't there?
Hiya,
I'm playing audio files from the streaming assets folder.
If the file I'm looking for isn't there, I don't want an error to be thrown, but instead I just want to log a message to the console.
I can't work out how to stop an Invalid Operation Exception error being thrown. Does anyone know how to do this?
This is how I'm reading the files:
public static string GetFileLocation(string relativePath)
{
string filename = relativePath + ".wav";
return "file://" + Path.Combine(Application.streamingAssetsPath, "Audio", filename);
}
IEnumerator AudioPlayer(string audioPath)
{
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(GetFileLocation(audioPath), AudioType.WAV))
{
yield return uwr.SendWebRequest();
if(uwr.isHttpError || uwr.isNetworkError)
{
Debug.Log("error");
}
if (audioSource.clip != null)
{
audioSource.Stop();
AudioClip currentClip = audioSource.clip;
audioSource.clip = null;
currentClip.UnloadAudioData();
DestroyImmediate(currentClip, false);
}
if (uwr != null)
{
audioSource.clip = DownloadHandlerAudioClip.GetContent(uwr);
audioSource.Play();
}
yield return null;
}
}
Thanks, Laurien
Comment