Here I have a method that adds a video layer to a UIView that I have set to IB:
-(void)loadPlayer{
self.asset = [AVAsset assetWithURL:url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:self.asset];
self.player = [AVPlayer playerWithPlayerItem:item];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
item.videoComposition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:self.asset];
[self.player pause];
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.videoLayer.layer addSublayer:self.playerLayer];
});
}
The above method is called in the viewDidAppear of the View controller, so every time the current View controller loads, the video should start.
Here I check the status of the player and play the asset, if he is ready:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object == self.player && [keyPath isEqualToString:@"status"]) {
if (self.player.status == AVPlayerStatusReadyToPlay) {
[self.player play];
}
}
}
The problem is that the view really appeared, the beginning of video playback, but only audio, not video. All I get is a black screen instead of a video. The sound plays perfectly, but it does not seem to display the video frames for some reason is unknown.
This only happens on iOS 10 devices, the same code when run on iOS 9 devices works like a charm. The video is displayed as expected.
-, AVFoundation iOS 10 AVPlayer ? -, , iOS 9 .
- , , . .
.
---- **** **** ----
! , - iOS 10 viewDidLayoutSubviews , iOS 9. viewDidLayoutSubviews. , . , loadPlayer . . , -.
, viewDidLayoutSubviews loadPlayer, :
self.playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);