简体   繁体   English

使用Bundle主URL无法从文件中找到URL

[英]Can't find URL from files using Bundle main url

I'm trying to play some small .wav files using AudioToolBox library, but I just can't get the url for the files that I dragged into the project folder (the folder which contains the classes, etc.) 我正在尝试使用AudioToolBox库播放一些小的.wav文件,但我无法获取我拖入项目文件夹的文件的URL(包含类的文件夹等)

if let soundUrl = Bundle.main.url(forResource: name, withExtension: "wav") {
    //make use of AudioServicesCreateSystemSoundID...
}
else {
    print("sound wasn't found")
}

I tried all combinations of possible drags to xcode (Copy itens if needed, Create groups, Create folder references) and very frequently I Clean the project, but still I always get "sound wasn't found". 我尝试了所有可能的拖动组合到xcode(如果需要复制它,创建组,创建文件夹引用),并且经常我清理项目,但仍然总是得到“声音未找到”。 I truly don't know what information to give, but my approach was working until some days ago, then it suddenly stopped... 我真的不知道要提供什么信息,但是我的方法在几天前工作,然后它突然停止了......

PS: Not sure if it's a useful information but I printed the list of files at Bundle.main.bundlePath and I didn't find any of the files I wanted, although they all appear in the list of files in the left (that contains codes, xcassets, Main.storyboard, etc.) PS:不确定它是否是一个有用的信息,但我在Bundle.main.bundlePath上打印了文件列表,但我没有找到我想要的任何文件,尽管它们都出现在左边的文件列表中(包含代码,xcassets,Main.storyboard等)

  • Click at one of your wav files 单击您的一个wav文件
  • At the right side, there will be a list of options 在右侧,将有一个选项列表
  • The last option of this list is a tab called Target Membership 此列表的最后一个选项是名为Target Membership的选项卡
  • Check if this wav file you chose marks your current project as target 检查您选择的此wav文件是否将当前项目标记为目标

hope that helps :) 希望有帮助:)

Daniel's answer is right in this case. 在这种情况下,丹尼尔的答案是正确的。 However this message can also occur because the code is in a framework bundle that is part of the project's workspace. 但是,此消息也可能发生,因为代码位于作为项目工作空间一部分的框架包中。 In this case, Bundle.main will get you the main project bundle and you should instead use a different bundle constructor, such as Bundle(identifier: <id>) or Bundle(for: type(of: <object>)) (where object is a class appearing in the bundle you want to load from). 在这种情况下, Bundle.main将为您提供主项目包,您应该使用不同的包构造函数,例如Bundle(identifier: <id>)Bundle(for: type(of: <object>)) (其中) object是出现在要加载的bundle中的类。

If anyone is getting this error while using react native, make sure that you've added your model to xcode workspace (xcode -> project's left side menu -> add files to "Yout Prj" -> select your file). 如果有人在使用react native时遇到此错误,请确保已将模型添加到xcode工作区(xcode - >项目的左侧菜单 - >将文件添加到“Yout Prj” - >选择您的文件)。 I had this error when I added my model in vs code and xcode didnt know where to find. 当我在vs代码中添加我的模型并且xcode不知道在哪里找到时,我遇到了这个错误。

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

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