如何在iPhone sdk中播放两种不同的声音?

时间:2011-09-22 15:40:19

标签: avaudioplayer iphone-sdk-4.3

我有两个不同的wav文件,我必须通过调用不同的方法来播放。我可以播放一个wav文件,但无法播放。我的代码是

在.h

AVAudioPlayer *player1, *player2;

在.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];
}

更新 Play multiple audio files using AVAudioPlayer 我看过这个链接,但对我没用。如何才能做到这一点?代码示例将不胜感激。 在控制台中,出现以下错误说明。

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

请帮我解决这个问题。在此先感谢

1 个答案:

答案 0 :(得分:0)

我通过将WAV格式转换为MP3来纠正自我。现在正确播放。