我为视频播放编写了AVPlayer的自定义播放器。根据苹果文档,设置视频图层:
self.player = [IPLPlayer new];
self.player.playerLayer = (AVPlayerLayer *)self.playerView.layer;
假设 self.playerView 是文档中的一个通常类:
@implementation PlayerView
+ (Class) layerClass {
return [AVPlayerLayer class];
}
- (AVPlayer *)player {
return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *) player {
[(AVPlayerLayer *) [self layer] setPlayer:player];
}
问题是:
当关闭应用程序(Home按钮)或锁定屏幕时,视频播放会停止,仅音频播放恢复,屏幕上的图像仍然是锁定屏幕之前的 - 它完全静态且不更改帧。如何在屏幕锁定后恢复视频播放?
似乎我必须注册通知,在应用程序变为活动状态后恢复视频层:
-(void)registerNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterBackground)
name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterForeground)
name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void)unregisterNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)willEnterBackground
{
NSLog(@"willEnterBackground");
[self.playerView willEnterBackground];
}
-(void)didEnterForeground
{
NSLog(@"didEnterForeground");
[self.playerView didEnterForeground];
}
viewDidDisappear
是移除观察者的好地方。 - Harish