我在乐器中看到,当我通过苹果提供的标准“AddMusic”示例方法播放声音时,它会通过
prepareToPlay
调用(引用
AudioToolBox
框架的
Cache_DataSource::ReadBytes
功能)每次分配新播放器时(即每次播放不同的声音)。但是,缓存的数据永远不会被释放。
你们有没有遇到过这个问题,或者我在代码中做错了什么?我有这个问题已经有一段时间了,它真的困扰着我,因为我的代码是什么苹果是逐字逐句(我想)。
如何调用函数:
- (void)playOnce:(NSString *)aSound {
// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = soundURL;
[soundURL release];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil];
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new m4a file is loaded
[newAudio release]; // release the audio safely
// this is where the prior cached data never gets released
[theAudio prepareToPlay];
// set it up and play
[theAudio setNumberOfLoops:0];
[theAudio setVolume: volumeLevel];
[theAudio setDelegate: self];
[theAudio play];
}
theAudio
在监狱里被释放
dealloc