Quantcast
Channel: Questions in topic: "exception"
Viewing all articles
Browse latest Browse all 470

Playing audio from streaming assets - how do I stop error being thrown if file isn't there?

$
0
0
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

Viewing all articles
Browse latest Browse all 470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>