- Home /
Loading .wav on iOS with WWW
I am trying to load a .wav using the WWW class. This works fine on Windows (PC) and Android. But on my iPhone 4 the app freezes when calling the method below (it stays in the while loop).
Does someone have an idea about what is going wrong?
private Track LoadTrack(string pathname)
{
WWW audioToLoadPath = new WWW("file://" + pathname);
while (!audioToLoadPath.isDone)
{
//Wait untill it's done
}
Track loadedAudio = Instantiate(TrackInstantiator) as Track;
loadedAudio.GetAudioSource().clip = audioToLoadPath.GetAudioClip(true, false);
return loadedAudio;
}
The pathname i am passing is retrieved by something like this (simplified exampe):
DirectoryInfo info = new DirectoryInfo(Application.persistentDataPath);
string pathname = persistentInfo.GetDirectories ()[0].FullName;
Edit: The path put in the WWW class is: file:///var/mobile/Applications/APP_GUID/Documents/file3.wav
XCode is not giving me any hints/debug information about this. Also audioToLoadPath.error is empty.
Other things i have tried:
Remove 'File://'
Tried it with 'File://' and 'File:///'
Tried 'File://localhost/var/...'
Is xCode saying anything? Also, you shouldn't need the "File://" since you are using persistentDataPath which already contains all you need to reach the folder.
I'll check what xCode sais. When i remove 'File://' it doesn't work on Windows nor on Android. I'll give it a try on my iPhone, just to be sure...
Have you tried putting a yield return null; in the while area? I am not sure it is actually waiting for it to finish.
I used to not have a timeout, but I have waited for atleast 10 seconds and still nothing happened. On android the files load almost directly, so I can't imagine it should take so much time on iOS
Answer by sumeeton · Mar 30, 2015 at 09:05 AM
You should always use coroutine when downloading files using WWW
IEnumerator LoadTrack(string pathname)
{
WWW www = new WWW(System.IO.Path.Combine("file://", pathname);
yield return www;
if(www.error != null)
{
Debug.Log("Couldn't Load file");
}
else
{
Track loadedAudio = Instantiate(TrackInstantiator) as Track;
loadedAudio.GetAudioSource().clip = www.GetAudioClip(true, false);
}
}
// Start the loading using...
StartCoroutine(LoadTrack(pathname));
Note that it's not just the use of a coroutine, it's also the yield return www
that's important here. You don't need to explicitly "wait until it's done", the yield does that for you. There have been questions on this here before, suggesting that this is more critical on iOS than other platforms.
Thanks! I will try this and will post here how it went.
It worked! Thank you very much!! I am starting to like couroutines more and more :)
One small thing: System.IO.Path.Combine("file://", pathname), does not work if 'pathname' is an absolute path (like C:/Documents/file.wav). It won't be a problem on Android or iOS but it was on Windows. Besides that, the code you send worked with almost no changes.
Your answer
Follow this Question
Related Questions
Is there a way to fix uploadprogress on mobile? 1 Answer
Ios problem with www class 2 Answers
Trying to load and play saved .wav from /Documents iOS 2 Answers
WWW yield is not returning any value in ios for yahoo finance api 2 Answers
how to load textures bigger than size limit through WWW? 1 Answer