インストゥルメントでは、Appleが提供する標準の「AddMusic」サンプルメソッドを使用してサウンドを再生すると、新しいプレーヤーが割り当てられるたびに(つまり、フレームワークの機能prepareToPlay
を参照する)呼び出しを介して32kbのメモリが割り当てられることがわかります。別の音が鳴ります)。ただし、そのキャッシュされたデータが解放されることはありません。AudioToolBox
Cache_DataSource::ReadBytes
これは、リリースされず、再生するサウンドファイルが多い場合、明らかに大きな問題を引き起こします。これは、メモリを割り当て続ける傾向があり、十分な数の固有のサウンドファイルがある場合は最終的にクラッシュするためです(残念ながらそうします)。
これに遭遇した人はいますか、それとも私のコードで何が間違っているのですか?私はしばらくの間この問題を抱えていましたが、私のコードはAppleのものと逐語的であるため、それは本当に私を悩ませています(私は思います)。
関数の呼び出し方法:
- (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