我有以下代码:
。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。
但是我不知道如何用声音文件来做到这一点。我想“链接”图像和相关的声音文件。文件中有数字,例如front1,front2 ...和无声的有sound1,sound2 ...
感谢帮助!
编辑:类似的东西
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sfx%ld.wav", (long) (value+1)] error:NULL];
不起作用
最佳答案
尝试以下
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];
关于ios - 如何增加声音文件(如uiimagefile)的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29743146/