- Home /
To sanitize or not to sanitize (WWW.GetAudioClip)
Hello guys,
I'm a bit confused right now by the WWW class, specifically concerning illegal characters in a local path ( to be accessed by GetAudioClip ). Loading audio with unsanitized slashes ( / or \ ) seems to work fine- actually, sanitizing slashes seems to result in a URL Malformed error. However, without sanitizing the url, an audio file with illegal characters won't be loaded due to a generic "Couldn't open file" error.
So, to clarify,
file:///Users/user/Music/iTunes/iTunes Media/Music/Stateless/Stateless/01 Prism #1.aif
won't play (due to the pound sign, I believe- a "Couldn't open file" error), but
file%3a%2f%2f%2fUsers%2fuser%2fMusic%2fiTunes%2fiTunes+Media%2fMusic%2fStateless%2fStateless%2f01+Prism+%231.aif
doesn't play either, due to a "Could not resolve host" error (or, unescaping the "file://", a "Couldn't open file" error).
In the documentation, it says "Note: URLs passed to WWW class must be '%' escaped.", so I am lead to believe that escaping should be necessary. I don't really know what to do about this, or how to load audio with illegal characters, but I am hoping that someone will have some tips.
Loading audio without sanitizing the url works as expected (unless a non-slash illegal character is used in the path), and I am doing so with the following code:
WWW www = new WWW ( "file://" + songToLoad );
yield return www;
UnityEngine.Debug.Log ( "www: " + www.url );
manager.audio.clip = www.GetAudioClip ( false, false, audioType );
UnityEngine.Debug.Log ( "Audio clip: " + manager.audio.clip.name );
However, sanitizing the path WWW www = new WWW ( "file://" + WWW.EscapeURL ( songToLoad ));
results in a "Couldn't open file" error. Escaping the path before loading, then unescaping before fetching the audio also provides an error before the audio can actually be found.
TL;DR
It seems like escaping doesn't work at all with the WWW class, so how should I load audio with an illegal character in it's name (the #, for instance)?
Thanks for your time, Gibson!
How are you encoding the URI? I think you aren't supposed to encode any / that is actually a path separator.
Did you look at the library call for it?
Thanks for pointing me to the documentation, I haven't taken a look at that yet.
I tried encoding the path using Unity's WWW.EscapeURL, but that seemed to make things worse -other than that, I am not encoding it (Unity may be doing something behind the scenes, but I don't know). The code that I pasted in my answer is exactly what I'm using to load audio. I don't think that the slashes should be encoded, either, but I don't think how to deal with characters like " # ", or " % ".
Your answer
![](https://koobas.hobune.stream/wayback/20220613152435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is there a way to send an XMLHttpRequest and get the response in a WWW object? 0 Answers
www.text + php + c# issue 2 Answers