我的代码设置了一个playernode mixernode和一个输入节点。我还将其设置为处理中断或功能的特定更改。
然而,这些更改带来了一个大问题,那就是如果视图控制器被解除,它不仅不再为我的应用程序释放内存,而且播放器节点继续播放。如果我回到播放器节点的视图并按下停止它不起作用。我甚至可以再次激活播放器,它将与无法停止的播放器一起播放。
-(void)setupAudioOne
{
NSError *error;
BOOL success = NO;
[self initAVAudioSession];
_isSessionInterrupted = NO;
_isConfigChangePending = NO;
_player = [[AVAudioPlayerNode alloc] init];
_inputOne = [[AVAudioInputNode alloc] init];
_setReverb = [[AVAudioUnitReverb alloc] init];
NSURL *hiphopOneURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Hip Hop 1" ofType:@"caf"]];
AVAudioFile *hiphopOneFile = [[AVAudioFile alloc] initForReading:hiphopOneURL error:&error];
_playerLoopBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:[hiphopOneFile processingFormat] frameCapacity:(AVAudioFrameCount)[hiphopOneFile length]];
success = [hiphopOneFile readIntoBuffer:_playerLoopBuffer error:&error];
NSAssert(success, @"couldn't read buffer bitch, %@", [error localizedDescription]);
_isRecording = NO;
[self createEngineAndAttachNodes];
[self makeEngineConnections];
_setReverb.wetDryMix = 75;
[_setReverb loadFactoryPreset:AVAudioUnitReverbPresetMediumHall];
//get notifications from the engine if there's a hardware config change
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioEngineConfigurationChangeNotification object:nil
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
// if we've received this change rewire everything
_isConfigChangePending = YES;
if (!_isSessionInterrupted) {
NSLog(@"Received a %@ notification!", AVAudioEngineConfigurationChangeNotification);
NSLog(@"Re-wiring connections and starting once again");
[self makeEngineConnections];
[self startEngine];
}
else {
NSLog(@"Session is interrupted, deffering changes");
}
//post notification
if ([self.delegate respondsToSelector:@selector(engineConfigurationHasChanged)]) {
[self.delegate engineConfigurationHasChanged];
}
}];
[self startEngine];
}
问题区域位于代码的底部区域,如果硬件配置发生变化,我会尝试从引擎获取通知。如果我删除此代码,当视图被取消时,引擎正在使用的所有内存也将被删除。但这也意味着,如果有人插入耳机应用程序崩溃。 所以我需要你看到的代码,但我需要更改它,这样如果视图被解除,应用程序将释放正在使用的内存。
有什么建议吗?