gpt4 book ai didi

ios - AVAudioPlayer 被释放,而键值观察者仍然在其中注册

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:59 25 4
gpt4 key购买 nike

-(void) setupAVPlayerForURL:(NSString *)url
{
[appdelegate.sharedplayer stop];

appdelegate.sharedplayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[appdelegate.sharedplayer setVolume:appdelegate.volumeDelegate];

[appdelegate.sharedplayer play];
self.seekBarSlider.minimumValue = 0.0f;
self.seekBarSlider.maximumValue = appdelegate.sharedplayer.duration;
self.isPlaying=YES;
appdelegate.sharedplayer.delegate=self;

[self loadAlbumArt];
[appdelegate.sharedplayer addObserver:self forKeyPath:@"status" options:0 context:nil];
}

每当我运行这个应用程序时,除了第一次,我都会收到这个日志消息但是应用程序没有崩溃

An instance 0xc1693d0 of class AVAudioPlayer was deallocated while key value observers were still registered with it. 
Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger.

这是当前的观测信息:

<NSKeyValueObservationInfo 0xc194d90> (
<NSKeyValueObservance 0xc194e40: Observer: 0xc1697d0, Key path:
status, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0,
Property: 0xc194d70> )

我做错了什么??

最佳答案

您必须从观察者中移除self。当您调用 -stop 时,AVAudioPlayer 的实例将被释放(您创建了一个新实例)。但是您试图继续保留其值。

因此您尝试观察内存中不再存在的实例。

试试这个:

- (void)setupAVPlayerForURL:(NSURL *)url {
[[appdelegate sharedplayer] removeObserver:self];
[[appdelegate sharedplayer] stop];

// and so on
}

您的应用不会因为您访问 已释放的实例而崩溃。相反,系统会“崩溃”您的应用程序,因为被观察对象的内存地址(然后被释放)稍后可能会指向其他某个对象,这会导致将错误的观察消息发送给您的观察者。

关于ios - AVAudioPlayer 被释放,而键值观察者仍然在其中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com