0

我有这个代码:

NSString *songPathForm = @"http://www.example.com/file.wav";

NSString *song = [[NSBundle mainBundle]pathForResource:songPathForm ofType:nil];

但是歌曲最终是空的,我做错了什么?

4

3 回答 3

2

这里的“路径”是指本地文件系统上的文件路径。你给 NSBundle 的是一个 URL。NSBundle 通常用于获取应用程序本身中的文件(当您从 Finder 中选择“显示包内容”时会看到什么)。NSBunlde 也需要相对文件路径,而不是绝对路径(因为您不知道应用程序在哪里)。

于 2011-12-07T23:32:41.753 回答
1

pathForResource方法仅适用于包内的文件。例如,如果您想通过外部 URL 访问文件,您可以dataWithContentsOfURL:NSData类中使用。

于 2011-12-07T23:31:57.770 回答
0

To play a sound for which you know the URL, you should use code similar to the following one:

NSSound *sound = [[NSSound alloc] initWithContentOfURL: [NSURL URLWithString:songPathForm] byReference:NO];
[sound play];
于 2011-12-08T02:33:35.777 回答