Avaudioplayer는 메모리를 풀지 않습니다
-
19-09-2019 - |
문제
ivar avaudioplayer 변수를 초기화하고 재생 및 메모리를 해제하려고합니다. 릴리스 라인 후에는 0x0으로 설정되지 않습니다. NIL 라인 후에는 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;
해결책
잘,
Avaudioplayer는 오디오 파일을 비동기 적으로 재생합니다. 재생을 시작한 후 플레이어를 해제해서는 안되며 메모리 관련 오류로 이어질 수 있으며 오디오 파일도 재생되지 않을 수 있습니다.
avaudioplayerdelegate를 통해 플레이어를 해제해야합니다. 자세한 내용은 확인할 수 있습니다 대답도 대답하십시오.
도움이 되었기를 바랍니다.
감사,
Madhup
다른 팁
- (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 Method에서 다음으로 출시되었습니다.
[appSoundPlayer release];
제휴하지 않습니다 StackOverflow