我正在制作一款iPhone应用程序,其中包括有关吉他音阶的教程。我的目标是,我的功能会播放许多声音,直到音阶完成。
这是随机比例:F G A B C D E.我记录的声音如C.mp3,D,E,F,G,A,B,C2,D2,E2 ...... 2是更高的音符。这是我使用的代码:
- (IBAction) playScale {
NSLog(@"s: %i", s);
NSLog(@"p: %i", pressed);
[self.player prepareToPlay];
components = [self.scale componentsSeparatedByString:@", "];
ns_NoteToPlay = [components objectAtIndex:s];
NSString *path = [[NSBundle mainBundle] pathForResource:ns_NoteToPlay ofType:@"m4a"];
NSLog(@"Path : %@", path);
self.player.delegate = self;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[self.player play];
mChord.text = ns_NoteToPlay;
NSLog(@"Nombre de notes ds Array : %i", components.count);
if ( s == 0) {
int clean;
clean = components.count + 1;
[NSTimer scheduledTimerWithTimeInterval:clean target:self selector:@selector(cleanDots) userInfo:nil repeats:NO];
}
nsNowPlayingNote = ns_NoteToPlay;
[self instrument];
s = s+1;
if ((s < components.count) && (pressed == 0)) {
[self performSelector:@selector(playScale) withObject:nil afterDelay:0.8];
}
}
这很有效。该功能播放音阶,但我找不到告诉何时调用高音C2的方法。因此,当它播放D然后C时,C应该是C2,但仍然调用C,这是较低的。
答案 0 :(得分:0)
你可以保留一系列NSStrings,其中包含你的文件名,如:
[NSArray arrayWithObjects:@"C", @"D"..., @"C2", @"D2", nil]
然后你可以保留一个int
变量,用于迭代数组。
作为旁注,您确定AVAudioPlayer
是播放声音的正确选择吗?你应该至少看看AudioServicesPlaySystemSound()
,这更适合播放短音。