Reputation: 3500
I'm using Blocks in iOS5 with ARC to play a never ending cycle of audio recordings with AVAudioPlayer. I alloc a new AVAudioPlayer after receiving the AVAudioPlayerDidFinishRecording message, which starts a cycle of recursion. My app consistently crashes after 120 or so audio clips are played back. I can see that it uses up more and more memory as each audio file is loaded, but never releases the memory.
I'm using a single AVAudioPlayer property, so I was expecting ARC to clean up the old instances every time I alloc a new player.
Could the blocks be somehow causing the old AVAudioPlayers to stick around?
Would it be better to use an Observer to monitor the player and launch/alloc new players?
Thanks so much for your help!
-(void)playNextAudioItem{
//get ready to load audio doc from iCloud
NSURL *ubiquitousContainer = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if(ubiquitousContainer){
//query iCloud
query = [[NSMetadataQuery alloc]init];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(%K == %@)", NSMetadataItemFSNameKey, [NSString stringWithFormat:@"%@.caf", audioItemGUID]];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter] addObserverForName:NSMetadataQueryDidFinishGatheringNotification object:query queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif){
//iCloud query finished gathering data
[query disableUpdates];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
NSMetadataItem *item1 = [query resultAtIndex:0];
audioCurrentVerse = [[audioDoc alloc] initWithFileURL:[item1 valueForAttribute:NSMetadataItemURLKey]];
//open the audio file
[audioCurrentVerse openWithCompletionHandler:^(BOOL success) {
if(success){
//configure the session and play the audio file
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:nil];
NSError *error = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer = [[AVAudioPlayer alloc] initWithData:audioCurrentVerse.data error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.delegate = self;
if([audioPlayer prepareToPlay]){
[audioPlayer play];
}
}
}];
[query stopQuery];
query = nil;
}];
[query startQuery];
}
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
//get an identifier for the next audio item
audioItemIndex++;
if(audioItemIndex >= rsAudioItems.recordCount){
verseIndex = 0;
}
audioItemGUID = [rsAudioItems getRow:audioItemIndex andColumnNamed:@"audioItemGUID"];
//recursive call to play the next audio item
NSLog([NSString stringWithFormat:@"%d", playCount++]);
[self playNextAudioItem];
}
Upvotes: 0
Views: 468
Reputation: 10782
Blocks take a snapshot of the stack that is not released until the block is freed. Don't use recursive blocks (or really recursion at all, if it doesn't really make sense in the situation) because the blocks never get freed so the snapshots stick around forever.
Using a for loop would alleviate this issue. You could add them to a serial dispatch queue and then they'd be freed after execution.
Upvotes: 1