【问题标题】:Music Scale : loop function to play different notes音阶:循环播放不同音符的功能
【发布时间】:2011-10-08 18:31:32
【问题描述】:

我正在制作一个包含吉他音阶教程的 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,这是更低的。

【问题讨论】:

    标签: objective-c ios cocoa-touch audio avaudioplayer


    【解决方案1】:

    您可以保留一个包含文件名称的 NSStrings 数组,例如:

    [NSArray arrayWithObjects:@"C", @"D"..., @"C2", @"D2", nil]
    

    然后您可以保留一个 int 变量,用于遍历数组。

    附带说明,您确定AVAudioPlayer 是播放声音的正确选择吗?你至少应该看看AudioServicesPlaySystemSound(),它更适合播放短音。

    【讨论】:

    • 谢谢!我会尝试解决这个问题! :p
    • 解决了!在大型 NSArray 和多个 int/strings/array 操作的帮助下像魅力一样工作
    猜你喜欢
    • 2021-06-27
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多