我初始化一个ivar AVAudioPlayer变量,播放并试图释放它的内存。在释放线之后,它不会设置为0x0。在零行之后,它不会变为0x0。 AVAudioPlayer是否需要特殊的东西来释放它的内存?
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: @"mysound" ofType: @"aif" inDirectory:@"/"]] error: &err];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = .5;
[audioPlayer prepareToPlay];
[audioPlayer play];
[audioPlayer release];
audioPlayer = nil;
答案 0 :(得分:2)
那么,
AVAudioPLayer异步播放音频文件。您不应该在开始播放后释放播放器,这可能会导致一些与内存相关的错误,并且您的音频文件也可能无法播放。
您应该通过avaudioplayerdelegate释放播放器。有关详细信息,请查看此answer too。
希望这有帮助。
谢谢,
Madhup
答案 1 :(得分:1)
- (IBAction) playaction {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = newURL;
[newURL release];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self
);
// Activates the audio session.
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
}
这里,appSoundPlayer在头文件中声明为
AVAudioPlayer *appSoundPlayer;
它是声明和合成的属性,也以dealloc方法发布为
[appSoundPlayer release];