简体   繁体   中英

Unity: Loading mp3 from StreamingAsset Folder in iOS

I am trying to load a MP3 from the streamingasset folder in a unity project running under IOS. In the editor everything works fine, but on the iPhone the MP3 is not loading.

I know about the filepath issues, but I assumed to have done everything correctly. Here is the code:

string fileName = "mytestfile";

if (Application.platform == RuntimePlatform.IPhonePlayer) {
    path = "file:/" + System.IO.Path.Combine (Application.streamingAssetsPath, fileName + ".mp3");
} else {
    path = "file://" + Application.dataPath + "/StreamingAssets/" + fileName + ".mp3";
}

Debug.Log ("LOAD FROM STREAMING ASSET " + path);

// Start a download of the given URL
WWW request = new WWW (path);

// Wait for download to complete
yield return request;

// use request.audio 
AudioClip loadedMp3 = request.GetAudioClip (false, false);   
Debug.Log ("Result length " + loadedMp3.length);  

And here is the output in xcode:

LOAD FROM STREAMING ASSET file://var/mobile/Applications/{appid}/appname.app/Data/Raw/mytestfile.mp3

Result length 0

Does anyone have any idea what am I doing wrong? As I said, in the editor everything works fine.

I actually know this because I was using it today. streaming assets in iOS is called /Raw/. So you have to do a #if UNITY_IPHONE and reference /Raw/ in lieu of StreamingAssets. In android it gets even more convoluted. You have to reference your .jar!! Aren't you glad you're using iOS?

Also you don't need file://

if UNITY_EDITOR

Use StreamingAssets

XCode player build magically transforms your directory into Raw. Therefore during runtime your code must say this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM