我用 playernode 混合器节点和输入节点设置了我的代码。我还设置了它来处理中断或功能的特定变化。
然而,随着这些变化带来了一个大问题,那就是如果视图控制器被关闭,不仅它不再为我的应用程序释放内存,而且 playernode 继续播放。如果我回到播放器节点的视图并按停止它不起作用。我什至可以再次激活播放器,它会与无法停止的播放器一起播放。
-(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];
}
问题区域位于代码的底部区域,如果有硬件配置更改,我会尝试从引擎获取通知。如果我删除此代码,当视图被关闭时,引擎正在使用的所有内存也将被删除。但这也意味着,如果有人插入耳机,应用程序就会崩溃。所以我需要你看到的代码,但我需要更改它,以便如果视图被关闭,应用程序将释放正在使用的内存。
有什么建议吗?