Search code examples
iosobjective-cavaudioplayernstimer

AVAudioPlayer crashing when calling stop


I am trying to run 3 audio files for a specific amount of time then stop them. I can play all 3 audio files at the same time no worries, but when I try to stop them I receive this error:

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

This is what my code looks like to play the file:

- (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];
            }
        }
    }


}

And then this is what I do to stop the audio.

- (void)stopMusic {

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

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

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

Solution

  • The error message is describing the problem perfectly. Your NSTimer is calling stopMusic:. But there is no method stopMusic:. There is a method stopMusic. stopMusic is not the same as stopMusic: (with a colon). You must get the name exactly right.