2011-09-22 109 views
0

我有两个不同的wav文件,我必须通过调用不同的方法来播放。我可以播放一个wav文件,但无法播放该文件。我的代码是如何在iPhone SDK中播放两种不同的声音?

在.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.) 

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

回答

0

我纠正了我的自我通过转换WAV格式为MP3。现在它播放正确。