Kev*_*own 0 iphone objective-c nsurl ios
我目前有这个代码有效.我有两个问题.
Run Code Online (Sandbox Code Playgroud)- (void)playAnimal{ NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Sheep.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = 0; if (audioPlayer == nil) NSLog([error description]); else [audioPlayer play]; }
我对Objective-C很新,所以很多语法对我来说都是新的,也是一般的方法.谢谢你的耐心!
使用以下内容:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sheep" ofType:@"mp3"]];
NSURL *url = [NSURL fileURLWithPath:path];
Run Code Online (Sandbox Code Playgroud)
将资源复制到捆绑包时,它们不会保留其目录结构.只需按名称识别资源,它就会找到它的路径.
就像一个FYI一样,以下工作也是如此:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sheep.mp3" ofType:@""]];
NSURL *url = [NSURL fileURLWithPath:path];
Run Code Online (Sandbox Code Playgroud)
如果您有一组逻辑上在plist中组合在一起的资源,并且您以编程方式读取这些资源,这可能很有用.例如:
// reference to a dictionary with the entry 'Sheep.mp3'
NSString *resourceName = [dictionary objectForKey:item1];
NSString *path = [[NSBundle mainBundle] pathForResource:resourceName ofType:@""]];
NSURL *url = [NSURL fileURLWithPath:path];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1617 次 |
| 最近记录: |