IOS Load AudioClip from /Documents Folder

Hi,

I use this code for iOS path:

  • publicstaticstringGetiPhoneDocumentsPath()
  • {
  • string path =Application.dataPath.Substring(0,Application.dataPath.Length-5);
  • path = path.Substring(0, path.LastIndexOf(‘/’));
  • return path +“/Documents”;
  • }

Documents folder include many .wav files. I want to play this audioClips.

I try this code:

  • string filepath =Path.Combine(GetiPhoneDocumentsPath(),SongName);
  • AudioClipmy=(AudioClip)Resources.Load(filepath,typeof(AudioClip));
  • audio.clip =my;
  • audio.Play();

but didn’t work, music is didn’t play :frowning:

How can I do???

Resources.Load only works for audio in your Resources folder, not for user audio.

You should use the WWW class. It has a handy GetAudioClip function, and loads async:

Cheers,

Gregzo

xCode Error line:
UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
error:
libc++abi.dylib: terminate called throwing an exception

I try WWW.
Worked on computer(MacOs) but didn’t work on iOS :frowning:

my code:

void Start() {
string link = “file://” + GetiPhoneDocumentsPath () + “/” + soundName;
Debug.Log (link);

StartCoroutine(loadMusic(link));
}

void Update (){

if (musicLoader != null && musicLoader.isDone && !audio.isPlaying) {
audio.clip = musicLoader.GetAudioClip(false,false);
audio.Play();
}
}

publicstaticstringGetiPhoneDocumentsPath ()
{

string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);
path = path.Substring(0, path.LastIndexOf(‘/’));
return path + “/Documents/Raw”;

}

WWWmusicLoader;
IEnumerator loadMusic (stringlink){
musicLoader = newWWW (link);
yield return musicLoader;

}

What is wrong??

Use Application.persistentDataPath instead. Log it first ( running on the device, not the editor ) to make sure where it’s pointing, I forgot if it point to the Documents folder or one step higher.

Also, as with all things I/O, always check that the file exists. Use the System.IO namespace, it has great classes to work easily with the file system - File, FileInfo, Directory and DirectoryInfo are musts, so is Path.

Thank you gregzo :slight_smile: My code is working. My fault is filename invalid character. :slight_smile:
I use :

foreach(char c in System.IO.Path.GetInvalidFileNameChars() ) {
newPath = newPath.Replace(c, ‘_’);
}

And work :slight_smile: Thank you very much :slight_smile: