调用stop时AVAudioPlayer崩溃

时间:2013-12-20 00:59:51

标签: ios objective-c avaudioplayer nstimer

我尝试在特定时间内运行3个音频文件然后停止它们。我可以同时播放所有3个audop文件而不用担心,但是当我试图阻止它们时,我发现了这个错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController stopMusic:]: unrecognized selector sent to instance

这就是我的代码播放文件的样子。

- (void)backgroundTap {
    [customerNameTextField resignFirstResponder];
    [customerEmailTextField resignFirstResponder];


    if ((audioPlayer) || (audioPlayer2) || (audioPlayer3)) {
        NSLog(@"still playing");
    } else {

        [NSTimer scheduledTimerWithTimeInterval:10.0
                                         target:self
                                       selector:@selector(stopMusic:)
                                       userInfo:nil
                                        repeats:NO];



        // create audio session
        [[AVAudioSession sharedInstance] setDelegate: self];
        NSError *setCategoryError = nil;
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
        if (setCategoryError)
            NSLog(@"Error setting category! %@", setCategoryError);

        // path
        NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"tuiSong" ofType:@"mp3"];
        NSURL *soundURL = [NSURL URLWithString:soundPath];
        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
        audioPlayer.numberOfLoops = 0;
        [audioPlayer setVolume: 0.1];

        // path
        NSString *soundPath2 =[[NSBundle mainBundle] pathForResource:@"beachSong" ofType:@"mp3"];
        NSURL *soundURL2 = [NSURL URLWithString:soundPath2];
        NSError *error2;
        audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL2 error:&error2];
        audioPlayer2.numberOfLoops = 0;
        [audioPlayer2 setVolume: 0.06];

        // path
        NSString *soundPath3 =[[NSBundle mainBundle] pathForResource:@"CicadaSong" ofType:@"mp3"];
        NSURL *soundURL3 = [NSURL URLWithString:soundPath3];
        NSError *error3;
        audioPlayer3 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL3 error:&error3];
        audioPlayer3.numberOfLoops = 5;
        [audioPlayer3 setVolume: 0.05];

        // play bells
        if (!audioPlayer) {
            NSLog(@"localizedDescription : %@", [error localizedDescription]);
        } else {
            if (![audioPlayer isPlaying]) {
                [audioPlayer play];
            }
            if (![audioPlayer2 isPlaying]) {
                [audioPlayer2 play];
            }
            if (![audioPlayer isPlaying]) {
                [audioPlayer3 play];
            }
        }
    }


}

然后这就是我停止播放音频的方法。

- (void)stopMusic {

    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
        audioPlayer = nil;
    }

    if ([audioPlayer2 isPlaying]) {
        [audioPlayer2 stop];
        audioPlayer2 = nil;
    }

    if ([audioPlayer3 isPlaying]) {
        [audioPlayer3 stop];
        audioPlayer3 = nil;
    }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

错误消息正在完美地描述问题。您的NSTimer正在呼叫stopMusic:。但是没有方法stopMusic:。方法stopMusicstopMusicstopMusic:(带冒号)不同。你必须得到完全正确的名字。