我正在使用带有 ARC 的 iOS5 中的 Blocks 来使用 AVAudioPlayer 播放一个永无止境的录音循环。我在收到 AVAudioPlayerDidFinishRecording 消息后分配了一个新的 AVAudioPlayer,它开始了一个递归循环。我的应用程序在播放 120 个左右的音频片段后总是崩溃。我可以看到它在加载每个音频文件时占用了越来越多的内存,但从不释放内存。
我正在使用单个 AVAudioPlayer 属性,所以我希望 ARC 每次分配新播放器时都会清理旧实例。
这些块会以某种方式导致旧的 AVAudioPlayers 留下来吗?
使用观察者监控玩家并启动/分配新玩家会更好吗?
非常感谢你的帮助!
-(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];
}