AVAudioPlayer not playing sounds simultaneously

∥☆過路亽.° 提交于 2019-12-08 02:14:46

问题


I am trying to play a couple of sounds files at the same time for the AVAudioPlayer. I read a thread explaining how to do it @ AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting . It seems to be a weird issue as I when i try to only play one file at a time (when self.options.on is ON), it works, but when I try creating multiple instances (the else statements) and play them nothing plays. I checked to see if the outputfile URL is correct and it is, so what is the reason that it isn't working?

Thanks!

- (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];

            }
        } 
    }
}

NOTE I tried initializing with NSError check but i'm not getting any errors:

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

SOLUTION It seemed to be an issue with the initialization of my NSMutableArrays.

I changed them from:

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

to

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

and that fixed it


回答1:


I have faced the same while played a small file:

Here is my solution (example)

- (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];
}



回答2:


Instead of passing error as nil try this and check if initialization is working correct.

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


来源:https://stackoverflow.com/questions/20064041/avaudioplayer-not-playing-sounds-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!