AVAudioPlayer不能同时播放声音

时间:2013-11-19 05:40:41

标签: ios iphone objective-c avaudioplayer

我正在尝试为AVAudioPlayer同时播放几个声音文件。我读了一篇解释如何做到这一点的帖子@ AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting。这似乎是一个奇怪的问题,因为当我尝试一次只播放一个文件时(当self.options.on为ON时),它可以工作,但是当我尝试创建多个实例(else语句)并且不播放它们时播放。我检查了输出文件的URL是否正确,是的,那么它无法正常工作的原因是什么?

谢谢!

- (void)playTapped:(id)sender 
{
    if (!recorder.recording)
    {
        if(self.options.on)
        {
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setCategory:AVAudioSessionCategoryPlayback error:nil];

            NSArray *pathComponents = [NSArray arrayWithObjects:
                                       [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                       [NSString stringWithFormat:@"sound%i.m4a",last],
                                       nil];
            NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

            player = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
            NSLog(@"%@",player.url);
            [player setDelegate:self];
            [player play];
        }
        else
        {
            // trying to play all sounds at once
            for (NSString *str in self.soundsCreated){
                NSLog(@"%@",str);
                NSArray *pathComponents = [NSArray arrayWithObjects:
                                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                           [NSString stringWithFormat:@"sound%@.m4a",str],
                                           nil];
                NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

                AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
                NSLog(@"%@",audioPlayer.url);
                [audioPlayer prepareToPlay];
                [audioPlayer setDelegate:self];
                [audioPlayer play];

            }
        } 
    }
}

我尝试使用NSError检查初始化,但我没有收到任何错误:

NSError *sessionError = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:&sessionError];
if (sessionError)
{
    NSLog(@"Error in audioPlayer: %@",[sessionError localizedDescription]);
}

这似乎是我的NSMutableArray的初始化问题。

我改变了它们:

self.soundsCreated = [[NSMutableArray alloc]init];
self.playerArray = [[NSMutableArray alloc]init];

self.soundsCreated = [NSMutableArray new];
self.playerArray = [NSMutableArray new];

并修复了它

2 个答案:

答案 0 :(得分:0)

我在播放小文件时遇到了同样的问题:

这是我的解决方案(示例)

- (void)dropSoundWhileRefresh
{
    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
    [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
    [[AVAudioSession sharedInstance] setDelegate:self];
    NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"waterDrops" ofType:@"mp3"]];
    dropSound = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [dropSound setDelegate:self];
    [dropSound setVolume:0.10];
    [dropSound setNumberOfLoops:0];
    [dropSound play];
}

答案 1 :(得分:0)

而不是将错误传递为nil尝试此操作并检查初始化是否正常工作。

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error) 
{
   DebugLog(@"Error in audioPlayer: %@",[error localizedDescription]);
}