Alg*_*der 2 objective-c avaudioplayer ios avplayer
-(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];
}
Run Code Online (Sandbox Code Playgroud)
每当我运行此应用程序时,我会收到此日志消息,但第一次但应用程序没有崩溃
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.
Run Code Online (Sandbox Code Playgroud)
这是目前的观察信息:
<NSKeyValueObservationInfo 0xc194d90> (
<NSKeyValueObservance 0xc194e40: Observer: 0xc1697d0, Key path:
status, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0,
Property: 0xc194d70> )
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
你必须self从观察者中删除.当您调用时-stop,AVAudioPlayer获取释放的实例(您创建一个新实例).但是你试图继续保留它的价值观.
因此,您尝试观察内存中不再存在的实例.
试试这个:
- (void)setupAVPlayerForURL:(NSURL *)url {
[[appdelegate sharedplayer] removeObserver:self];
[[appdelegate sharedplayer] stop];
// and so on
}
Run Code Online (Sandbox Code Playgroud)
您的应用程序没有崩溃,因为您访问了已取消分配的实例.而是系统"崩溃"你的应用程序,因为观察对象的内存地址(然后被释放)可能会稍后指向其他对象,这会导致错误的观察消息被分派给你的观察者.