- Home /
How can I put a mp3 in the iphone documents folder then load it with www?
Is it possible to manually (or using some scripting) put a file in the iphone documents folder and call it up using the WWW protocol.
How do I put the file in? How do I get the path to the file?
Thanks, Dan
Answer by mpavlinsky · Mar 08, 2012 at 07:26 PM
Certainly.
You can get a path to a writeable directory like so:
string writeablePath = Application.dataPath.Substring(0, "/var/mobile/Applications/XXXXYYYY-XXXX-XXXX-XXXX-XXXXYYYYZZZZ/".Length) + "Documents/Raw";
Then you can use System.IO.Files to read and write the files in that directory:
System.IO.File.WriteAllBytes(writeablePath + "/mydocument.mp3", bytes);
byte[] mp3Bytes = System.IO.File.ReadAllBytes(writeablePath + "/mydocument.mp3");
And WWW:
WWW mp3WWW = new WWW("file://" + writeablePath + "/mydocument.mp3");
while (mp3WWW.isDone != true)
{
// Show progress or something.
yield return null;
}
if (mp3WWW.error == null)
{
byte[] mp3Bytes = mp3WWW.bytes;
// Do something cool.
}
Thanks for the complete code examples. Where do I get the ID code for XXXXYYYY-XXXX-XXXX-XXXX-XXXXYYYYZZZZ?
It's already in Application.dataPath. I just put those as placeholder to show I wanted to keep that part of the string. I know how many digits and dashes there are, but I don't know exactly what they are since it's your app identifier or whatever. It could just as easily be (Application.dataPath.Substring(0, 62) + "Documents/Raw";) I just wanted to be a little more clear in my intent.
So the docs for datapath say //Data. So could I just stripoff my appname+".app/Data" from the entire string to get the right substring?
I'm getting the hang of it. I copied a file from my assets folder to the documents folder using just the system io commands. How do I let the user know the progress or whether it's completed or not. I know www has progress, and isDone but I can't use those. Right?
What's the recommended way to load the mp3 as an audio source clip. Should I use WWW? It says no strea$$anonymous$$g mp3 in the editor so I can't really test it in the editor. Is there an alternative way to load an audio clip using system io and assign it to the audio source. $$anonymous$$aybe like audio.clip=the path to the file? I can't use Resources.Load on the iphone right?