您可以创建两个 NSMutableArrays 并在点击记录时清空它们。您还需要一个 NSTimer 和一个 int。所以在标题中:
NSTimer *recordTimer;
NSTimer *playTimer;
int incrementation;
NSMutableArray *timeHit;
NSMutableArray *noteHit;
在您的标题中包含所有的空隙和 IBActions 等等。
让你的声音按钮都有不同的、独特的标签。
然后在你的主文件中:
-(void)viewDidLoad {
timeHit = [[NSMutableArray alloc] init];
noteHit = [[NSMutableArray alloc] init];
}
-(IBAction)record {
recordTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(timerSelector) userInfo:nil repeats:YES];
[timeHit removeAllObjects];
[noteHit removeAllObjects];
incrementation = 0;
}
-(void)timerSelector {
incrementation += 1;
}
-(IBAction)hitSoundButton:(id)sender {
int note = [sender tag];
int time = incrementation;
[timeHit addObject:[NSNumber numberWithInt:time]];
[noteHit addObject:[NSNumber numberWithInt:note]];
[self playNote:note];
}
-(IBAction)stop {
if ([recordTimer isRunning]) {
[recordTimer invalidate];
} else if ([playTimer isRunning]) {
[playTimer invalidate];
}
}
-(IBAction)playSounds {
playTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(playback) userInfo:nil repeats:YES];
incrementation = 0;
}
-(void)playback {
incrementation += 1;
if ([timeHit containsObject:[NSNumber numberWithInt:incrementation]]) {
int index = [timeHit indexOfObject:[NSNumber numberWithInt:incrementation]];
int note = [[noteHit objectAtIndex:index] intValue];
[self playNote:note];
}
}
-(void)playNote:(int)note {
//These notes would correspond to the tags of the buttons they are played by.
if (note == 1) {
//Play your first note
} else if (note == 2) {
//Play second note
} else if (note == 3) {
//And so on
} else if (note == 4) {
//etc.
}
}
稍微摆弄一下(我怀疑这段代码是否完美),你可能会让它工作。就像您可能希望在按下其中一个按钮后禁用播放/录制按钮一样。祝你好运!