我试图将音频文件加载到AVAudioPlayer上的iPad上。当我在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-应答机-1
path =/User/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39-47B9-XXXX-1E3A2CE145D1/MyApp.app/st-answermachine-1.m4a
url = (null)
但如果我在设备上试一试,我就会得到这个:
名称=st-应答机-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
知道我为什么会有这个问题吗?
谢谢!
发布于 2010-11-16 14:43:06
File:///blah‘). URLWithString:需要一个包含实际URL的字符串作为它的参数(例如'http://blah/’或‘http://blah/’URL不能包含空格(就像模拟器的路径一样),这就是它失败的原因。
正如Evan建议的那样,您需要使用fileURLWithPath:将路径字符串转换为URL对象。
https://stackoverflow.com/questions/4194636
复制相似问题