Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 查找要在Objective C中播放的音频文件_Objective C - Fatal编程技术网

Objective c 查找要在Objective C中播放的音频文件

Objective c 查找要在Objective C中播放的音频文件,objective-c,Objective C,我是一个敏捷的家伙,回去做一些Objc工作,我正在尝试向我的应用程序添加声音,问题是,当试图定位文件时,路径为空 该文件位于myProj/Resources/Sounds/genericButtonPress.wav NSString *path = [[NSBundle mainBundle] pathForResource:@"myProj/Resources/Sounds/genericButtonPress" ofType:@"wav"]; NSLog(@"%@", pa

我是一个敏捷的家伙,回去做一些Objc工作,我正在尝试向我的应用程序添加声音,问题是,当试图定位文件时,
路径

该文件位于
myProj/Resources/Sounds/genericButtonPress.wav

    NSString *path = [[NSBundle mainBundle] pathForResource:@"myProj/Resources/Sounds/genericButtonPress" ofType:@"wav"];
    NSLog(@"%@", path); // NULL
    NSURL *url = [NSURL fileURLWithPath:path];

    AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
    [player play];
从文档中可以看出,
pathForResource
必须是绝对值,但我也尝试将
genericbutonpress
作为字符串

我想在这里得到一些帮助。谢谢

我的解决方案是

ViewController.h

#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>

@interface ViewController : UIViewController
@property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
@end
我的解决办法是

ViewController.h

#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>

@interface ViewController : UIViewController
@property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
@end
答案是:

记住,孩子们,总是要确保你的文件被分配给你的目标,否则事情不会出现,也不会起作用。尤其是字体和音频文件

答案是:


记住,孩子们,总是要确保你的文件被分配给你的目标,否则事情不会出现,也不会起作用。尤其是字体和音频文件

您是否尝试过
NSString*path=[[NSBundle mainBundle]pathForResource:@“genericButtonPress”类型:@“wav”]?是的。不走运。对于你的问题,你说
wav
文件名是
myound
。但在您的代码中,您正在查找
genericButtonPress
。你确定你没有用错名字吗?尝试
NSString*path=[[NSBundle mainBundle]pathForResource:@“wav”类型的“myProj/Resources/Sounds/mysound”。抱歉,这是一个编辑错误。我更新了原来的帖子。该文件名为
genericButtonPress.wav
ohok。另一件需要考虑的事情是,你在同一个项目中开发了一个框架和应用程序吗?因为如果将文件添加到框架的捆绑包中,则需要通过框架的捆绑包访问该文件。ObjC语法应该是这样的:
[NSBundle bundlewithintentifier:@“YourFrameworkName]”您是否尝试过
NSString*path=[[NSBundle mainBundle]pathForResource:@“wav”类型的“genericButtonPress”?是的。不走运。对于你的问题,你说
wav
文件名是
myound
。但在您的代码中,您正在查找
genericButtonPress
。你确定你没有用错名字吗?尝试
NSString*path=[[NSBundle mainBundle]pathForResource:@“wav”类型的“myProj/Resources/Sounds/mysound”。抱歉,这是一个编辑错误。我更新了原来的帖子。该文件名为
genericButtonPress.wav
ohok。另一件需要考虑的事情是,你在同一个项目中开发了一个框架和应用程序吗?因为如果将文件添加到框架的捆绑包中,则需要通过框架的捆绑包访问该文件。ObjC语法应该是这样的:
[NSBundle bundlewithintentifier:@“YourFrameworkName]”