I have a carousel object with a UIImageView where I am trying to subLayer the AvPlayerLayer.
It works perfectly fine though it doesn't fill the full bounds of the _videoView it appears to be smaller in height, though i have tested changing the height to extreme values and it still sits at that size as long as its overlapping the UIImageView.
The code I am using to achieve this is below:
UIView *embeddedView = [[UIView alloc] initWithFrame:_videoView.bounds];
embeddedView.center = CGPointMake(100, 140);
AVPlayerItem *avPlayeritem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[_videoURLS objectAtIndex:carousel.currentItemIndex]]];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayeritem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
[avPlayerLayer setFrame:embeddedView.frame];
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[avPlayerLayer setNeedsLayout];
[embeddedView.layer addSublayer:avPlayerLayer];
[carousel.currentItemView addSubview:embeddedView];
//Assign to notication to check for end of playback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:avPlayeritem];
[avPlayer seekToTime:kCMTimeZero];
[avPlayer play];
Here is an image of whats happening, as you can tell its off on the top as well as the bottom:
Whats causing this and how do I fix this?
I think that it's logic that the video doesn't fill the full bounds of the videoView
, because you set avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect
.
You should set it to AVLayerVideoGravityResizeAspectFill
, and it'll will be ok.
The difference is:
AVLayerVideoGravityResizeAspect
- show the video proportional to the original size, and don't allow any deformation.
AVLayerVideoGravityResizeAspectFill
- resize the video, to fill the full bounds of its subview, and don't care about proportionality.
And one last thing, do [avPlayerLayer setFrame:embeddedView.bounds]
it's better than [avPlayerLayer setFrame:embeddedView.frame]
.
Hope this helps :)