NSURL 在模拟器中为空,但在 iPad 上可以

发布于 2024-10-02 13:03:41 字数 1003 浏览 9 评论 0原文

我正在尝试将音频文件加载到 iPad 上的 AVAudioPlayer 中。当我在 iPad 上运行它时,它发现它在捆绑包中很好。但是,如果我尝试通过模拟器运行它,我会收到 NSURL 的空错误。这是代码片段(num 是任意 int):

NSString *name = [NSString stringWithFormat:@"st-answermachine-%i", num];
NSLog(@"name = %@", name);
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"m4a"];
NSLog(@"path = %@", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(@"url = %@", url);

在模拟器中,调试器控制台跟踪此:

名称 = st-answermachine-1

路径 = /Users/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39-47B9-XXXX-1E3A2CE145D1/MyApp.app/st-answermachine-1.m4a

网址=(空)

但是如果我在设备上尝试它,我会得到:

名称 = st-answermachine-1

路径 = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a

url = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a

请问我为什么会遇到此问题?

谢谢!

I'm trying to load an audio file into AVAudioPlayer on the iPad. When I run it on the iPad it finds it in the bundle fine. However, if I try and run it through the simulator, I get a null error for NSURL. Here's the snippet of code (num is an arbitrary int):

NSString *name = [NSString stringWithFormat:@"st-answermachine-%i", num];
NSLog(@"name = %@", name);
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"m4a"];
NSLog(@"path = %@", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(@"url = %@", url);

In the simluator, the Debugger Console traces this:

name = st-answermachine-1

path = /Users/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39-47B9-XXXX-1E3A2CE145D1/MyApp.app/st-answermachine-1.m4a

url = (null)

But if I try it on the device, I get this:

name = st-answermachine-1

path = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a

url = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a

Any ideas why I might have this problem please?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-10-09 13:03:41

URLWithString: 需要一个包含实际 URL 的字符串作为其参数(例如“http://blah/”或“file:///blah”)。 URL 不能包含空格(就像模拟器的路径一样),这就是它失败的原因。

正如 Evan 所建议的,您需要使用 fileURLWithPath: 将路径字符串转换为 URL 对象。

URLWithString: expects a string containing an actual URL as its parameter (e.g. 'http://blah/' or 'file:///blah'). URLs can't contain spaces (as the simulator's path does), and that's why it's failing.

As Evan suggests, you need to use fileURLWithPath: to convert a path string to a URL object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文