Search code examples
avaudioplayeriphone-sdk-4.3

How to play two different sounds in iPhone sdk?


I have two different wav files which I have to play by calling different methods. I can play one wav file but unable to play the one. My code is

In .h

AVAudioPlayer *player1, *player2;

In .m

-(void)correctSound {
    NSString *sound = [[NSString alloc] initWithFormat:@"claps2"];
    NSLog(@"sound claps %@", sound);
    NSURL *url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:sound ofType:@"wav"]];
    NSLog(@"claps url %@", url);
    player1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    player1.delegate = self;
    player1.volume = 1.0;
    [player1 prepareToPlay];
    [player1 play];

    [sound release];
    [url release];
}

-(void)wrongSound {
    NSString *sound1 = [[NSString alloc] initWithFormat:@"boo"];
    NSLog(@"sound boo %@", sound1);
    NSURL *url1 = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:sound1 ofType:@"wav"]];
    NSLog(@"boo url %@", url1);
    NSError *error;
    player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:&error];
    NSLog(@"localizedDescription = %@",[error localizedDescription]);
    player2.delegate = self;
    player2.volume = 1.0;
    [player2 prepareToPlay];
    [player2 play];

    [sound1 release];
    [url1 release];
}

Update Play multiple audio files using AVAudioPlayer I have seen this link, but It is not helpful to me. How can this be done? Code samples will be greatly appreciated. In console- the following error description appeared.

localizedDescription = The operation couldn’t be completed. (OSStatus error 1685348671.)

Please help me to over come this problem. Thanks in Advance


Solution

  • I corrected my self by converting the WAV format to MP3. Now its Playing correctly.