Ios 如何增加声音文件(如uiimagefile)的值

Ios 如何增加声音文件(如uiimagefile)的值,ios,objective-c,xcode,image,audio,Ios,Objective C,Xcode,Image,Audio,我有以下代码: h 不起作用pathForResource方法采用简单的NSString,而不是格式。这样做: NSString *name = [NSString stringWithFormat:@"sfx%ld.wav", (long) (value+1)]; NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil]; NSURL *url = [[NSURL alloc] in

我有以下代码:

h

不起作用

pathForResource方法采用简单的NSString,而不是格式。这样做:

    NSString *name = [NSString stringWithFormat:@"sfx%ld.wav", (long) (value+1)];
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url  error:nil];
试着跟随

NSString * strName = [NSString stringWithFormat:@"sfx%ld", (long) (value+1)];
NSString * strPath = [[NSBundle mainBundle] pathForResource:strName ofType:@"wav"];
NSURL * urlPath = [NSURL fileURLWithPath:strPath];
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:NULL];

我想我得说点什么,比如按按钮之类的?或者音频播放器是否从initwithframe继承了它?

self.audioplayer=[[AVAudioPlayer alloc]InitWithContentsURL:[[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@sfx%ld.wav,long value+1]]错误:NULL];谢谢!但是现在我得到一个错误:'AVAudioPlayer'没有可见的@interface声明选择器'initWithContentsOfURL:'我已经导入了导入和一个带有@property nonatomic AVAudioPlayer*audioplayer的属性;@vladimir:我们必须使用[[AVAudioPlayer alloc]initWithContentsOfURL:urlPath错误:NULL];而不是[[AVAudioPlayer alloc]initWithContentsOfURL:urlPath];谢谢。但在这里它崩溃了:**由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'***-[NSURL initFileURLWithPath:]:nil字符串参数'***检查strName的值。是否为nil?我的错误,它不再崩溃,在错误的示例项目中,呵呵…但它通过单击相关图像不会播放声音。我不是说他应该通过触摸播放示例吗?您是否调用[self.audioplayer play];是的,直接在initWithFrame方法中播放
    NSString *name = [NSString stringWithFormat:@"sfx%ld.wav", (long) (value+1)];
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url  error:nil];
NSString * strName = [NSString stringWithFormat:@"sfx%ld", (long) (value+1)];
NSString * strPath = [[NSBundle mainBundle] pathForResource:strName ofType:@"wav"];
NSURL * urlPath = [NSURL fileURLWithPath:strPath];
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:NULL];
-(id) initWithFrame: (CGRect)frame andPosition: (NSInteger)pos andValue: (NSInteger)value {
self = [super initWithFrame: frame];
if (self) {
    self.frontImage = [UIImage imageNamed:[NSString stringWithFormat: @"card-%ld.png", (long) (value+1)]];


    NSString * strName = [NSString stringWithFormat:@"sfx%ld", (long) (value+1)];
    NSString * strPath = [[NSBundle mainBundle] pathForResource:strName ofType:@"wav"];
    NSURL * urlPath = [NSURL fileURLWithPath:strPath];
    self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:NULL];
    [self.audioplayer play];

}
return self;
}