我有一个内置钢琴的应用程序。每个键(Button1)在按下时开始播放音符,当用户抬起手指时,我希望音符停止播放。发生的情况是,当停止功能设置为按键 (Button1) 上的 Touch Up Inside 时,音频不会停止播放。如果我将动作从钢琴键 (Button1) 更改为不同的按钮 (Button2),如果我按下按钮 2,它将停止播放。
。H
#import <AVFoundation/AVAudioPlayer.h>
@interface FifthViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theNote;
}
.m
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>
//White
-(IBAction)note1w:(id)sender { //Touch Down
[self whiteKey:self];
[self downKey:self];
NSString *path = [[NSBundle mainBundle] pathForResource:@"white1" ofType:@"aif"];
theNote = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theNote.delegate = self;
theNote.currentTime = 0;
[theNote play];
}
-(IBAction)stopMusic { //Touch Up Inside - This is the action that I change from the Button1 (The Piano Key) to Button2 (A Reg UIButton) and it works.
[theNote stop];
}