我创建了一个AVAudioUnitSampler
,但是在通过该Instruments
工具检查内存之后,似乎当场景发生变化时,即使AVAudioUnitSampler
对象为零,它最初加载的资源仍然在内存中。当我重新创建采样器时,它会重新加载资源,现在我有双倍的用于采样器的内存量。如何强制资源解除分配?
这是代码:
-(void) loadSampler {
// Instatiate audio engine
_engine = [[AVAudioEngine alloc] init];
_mixer = [_engine mainMixerNode];
_sampler = [[AVAudioUnitSampler alloc] init];
[self loadSoundFontInstrument]; //sound font is the instrument that the sampler will use to play sounds
[self makeEngineConnections];
[self startEngine];
}
-(void) loadSoundFontInstrument {
if (_sampler != nil) {
NSString* instrument = [[GameData sharedGameData].settings valueForKey:@"instrument"]; //decides on what sound font file to use
NSURL *piano = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:instrument ofType:@"sf2"]];
[_sampler loadSoundBankInstrumentAtURL:piano program:0 bankMSB:0x79 bankLSB:0 error:nil];
}
else
NSLog(@"ERROR: Sampler has not been initialized");
}
-(void)makeEngineConnections {
[_engine attachNode:_sampler];
[_engine connect:_sampler to:_mixer format:[_sampler outputFormatForBus:0]];
}
-(void)startEngine {
[_engine startAndReturnError:nil];
}