如何增加像 uiimagefile 这样的声音文件的值
How to increase the value of a sound file like an uiimagefile
我有如下一段代码:
.h
#import <UIKit/UIKit.h>
-(id) initWithFrame: (CGRect)frame andPosition: (NSInteger)pos andValue: (NSInteger)value {
self = [super initWithFrame: frame];
if (self) {
self.frontImage = [UIImage imageNamed:[NSString stringWithFormat: @"front%ld.png", (long) (value+1)]];
self.backImage = [UIImage imageNamed:@"back"];
}
return self;
}
@end
好的,看起来很简单。只是一个带有图像的框架。图像获得值 +1。
但我无法弄清楚如何使用声音文件来做到这一点。我想要 "link up" 图像和相关的声音文件。文件有编号,例如front1,front2... soundless 有 sound1, sound2...
感谢帮助!
编辑:类似于
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sfx%ld.wav", (long) (value+1)] error:NULL];
不起作用
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];
-(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;
}
我想我必须说点什么,比如触摸按钮之类的?还是音频播放器从 initwithframe 继承了它?
我有如下一段代码:
.h
#import <UIKit/UIKit.h>
-(id) initWithFrame: (CGRect)frame andPosition: (NSInteger)pos andValue: (NSInteger)value {
self = [super initWithFrame: frame];
if (self) {
self.frontImage = [UIImage imageNamed:[NSString stringWithFormat: @"front%ld.png", (long) (value+1)]];
self.backImage = [UIImage imageNamed:@"back"];
}
return self;
}
@end
好的,看起来很简单。只是一个带有图像的框架。图像获得值 +1。 但我无法弄清楚如何使用声音文件来做到这一点。我想要 "link up" 图像和相关的声音文件。文件有编号,例如front1,front2... soundless 有 sound1, sound2...
感谢帮助!
编辑:类似于
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sfx%ld.wav", (long) (value+1)] error:NULL];
不起作用
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];
-(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;
}
我想我必须说点什么,比如触摸按钮之类的?还是音频播放器从 initwithframe 继承了它?