iphone - URLWithString 对于资源路径返回 nil - iphone

标签 iphone resources nsurl

由于某种原因获取资源的 URL 时出现问题:此代码位于 viewDidLoad 中,并且在其他应用程序中有效,但由于某种原因在此处不起作用:

NSString* audioString = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
NSLog(@"AUDIO STRING: %@" , audioString);

NSURL* audioURL = [NSURL URLWithString:audioString];
NSLog(@"AUDIO URL: %d" , audioURL);

NSError* playererror;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&playererror];
[audioPlayer prepareToPlay];    

NSLog(@"Error %@", playererror);

日志输出:

音频字符串:/var/mobile/Applications/D9FA0569-45FF-4287-8448-7EA21E92EADC/SoundApp.app/sound.wav

音频网址:0

错误错误域=NSOSStatusErrorDomain代码=-50“操作无法完成。(OSStatus错误-50。)”

最佳答案

您的字符串没有协议(protocol),因此它是无效的 url 。试试这个...

NSString* expandedPath = [audioString stringByExpandingTildeInPath];
NSURL* audioUrl = [NSURL fileURLWithPath:expandedPath];

关于iphone - URLWithString 对于资源路径返回 nil - iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2112927/

相关文章:

java - android 中相同包名的问题

Android:具有具有完全相同 ID 的元素的不同布局 - 有效,但为什么呢?

ios - NSURL isFileURL 总是返回 NO

ios - 如何在 objective-c 中使用 nsurl 在 url 查询参数中传递数组?

iphone - Unity3d 和 iPhone Objective-c?

ios - Apple 可以告诉我用户购买了什么吗?

iphone - ButtonPressed 方法上的警告

iphone - 在 Objective-C 中查找选择器的方法签名

Django-Tastypie : How Do You Access (Http)request object in the Bundle?

ios - Objective-C 将 NSURL 传递给另一个 View Controller