我从 AVPlayer 编写自定义播放器用于视频播放。根据Apple docs设置视频层:
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];
}
问题是:当关闭应用程序(主页按钮)或阻止屏幕时,视频播放停止,并且当仅恢复音频播放恢复时,屏幕上的图像仍然是阻止屏幕之前的图像 - 它是完全静态的并且注意更改帧。
屏幕被阻塞后如何恢复视频播放?
似乎我必须注册通知,并在应用程序变为活动状态后恢复视频层:
-(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];
}