我面临一个非常奇怪的问题,我的要求是在单击按钮时播放声音,这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path=[[NSBundle mainBundle] pathForResource:@"button-3" ofType:@"mp3"];
tapSound=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]];
[tapSound prepareToPlay];
tapSound.delegate=self;
//creating the button in view did load and button is declared in .h
btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(x, y, 58,58);
[btn addTarget:self action:@selector(btnClkd:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//click method
-(void)btnClkd:(UIButton*)sender
{
[tapSound play];
}
但是当我运行代码并单击按钮时,我的应用程序崩溃并出现此错误:“由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[UIButton play]: unrecognized selector sent to instance 0x758c790'”
但是,如果我在其他任何地方播放声音而不是单击按钮,那么它会毫无问题地播放。我不知道为什么会这样?请帮我