简体   繁体   English

Unity:从iOS中的StreamingAsset文件夹加载mp3

[英]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. 我正在尝试从在IOS下运行的统一项目中的streamingasset文件夹中加载MP3。 In the editor everything works fine, but on the iPhone the MP3 is not loading. 在编辑器中,一切正常,但在iPhone上未加载MP3。

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: 这是xcode的输出:

LOAD FROM STREAMING ASSET file://var/mobile/Applications/{appid}/appname.app/Data/Raw/mytestfile.mp3 从流媒体资产加载文件:// var / mobile / Applications / {appid} /appname.app/Data/Raw/mytestfile.mp3

Result length 0 结果长度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/. iOS中的流媒体资产称为/ Raw /。 So you have to do a #if UNITY_IPHONE and reference /Raw/ in lieu of StreamingAssets. 因此,您必须执行#if UNITY_IPHONE并引用/ Raw /来代替StreamingAssets。 In android it gets even more convoluted. 在android中,它变得更加复杂。 You have to reference your .jar!! 您必须引用您的.jar! Aren't you glad you're using iOS? 您不高兴使用iOS吗?

Also you don't need file:// 你也不需要file://

if UNITY_EDITOR 如果是UNITY_EDITOR

Use StreamingAssets 使用StreamingAssets

XCode player build magically transforms your directory into Raw. XCode Player构建神奇地将您的目录转换为Raw。 Therefore during runtime your code must say this! 因此,在运行时,您的代码必须这样说!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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