1
votes

*This is my first stackoverflow question so apologies if I am doing something wrong.

I am trying to create a simple sound board app. I'm having trouble getting my AVAudioPlayer on xCode 4.1 (iOS 6.1.2) to update where the sound file to play is located. I wanted to avoid having multiple audio players (audioPlayer2, audioPlayer3, etc) so I am trying to use the same audio player but instead, update where the sound file is located. I only want one sound playing at a time so multiple sounds is obviously not an issue. Here is my code:

- (IBAction)play {
 if (audioPlayer.playing == NO) {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/k.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    //I added the next two lines to allow me to play AVAudio with MPiPodPlayer (which I found on this site, too) simultaneously.
    AVAudioSession *audiosession = [AVAudioSession sharedInstance];
    [audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];

    [audioPlayer play];
    [start setTitle:@"Stop" forState:UIControlStateNormal];
}
 else
     if (audioPlayer.playing == YES) {
         [audioPlayer stop];
     [start setTitle:@"Start" forState:UIControlStateNormal];
    }
    }


- (IBAction)play2 {
if (audioPlayer.playing == NO) {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/chicken.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    AVAudioSession *audiosession = [AVAudioSession sharedInstance];
    [audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];

    [audioPlayer play];
    [start2 setTitle:@"Stop" forState:UIControlStateNormal];
    clicked = 1;
    clicked2 = 0;
} else
    if (audioPlayer.playing == YES) {
        [audioPlayer stop];
        [start2 setTitle:@"Start" forState:UIControlStateNormal];
        clicked = 0;
    }
}

My IBActions are linked to UIButtons and the 'start's are UIButton references. Whenever I click on 'start' it plays 'k.mp3' fine, however if I then click on 'start2' AFTER it starts to play the 'k.mp3' file, and not the chicken file. Whichever button I click on first is what the url is set to. This is my first iPhone OS application project so I realize there are probably some embarrassing coding mistakes in there (feel free to correct me). I'd like an answer that would be applicable to multiple buttons, even for some 30 buttons so I do not have to copy and paste stop audio player 1, 2, and 3 for each button.

To summarize: I have multiple buttons that play one sound each, I would like no more than one sound playing at a time; when I click on a button it plays 1 sound and all other audio players stop. I would prefer having only one AVAudioPlayer instance for simplicity. Thanks in advance!

1

1 Answers

1
votes

Check if application control goes into - (IBAction)play2

And if control goes in it and it is still not working then you might try [audioPlayer release] before

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

that might do the trick for you