Stretch video on AVPlayerLayer
Asked Answered
C

1

6

I am using the following code to show a full-screen video on my UIView:

- (void)playMovie:(NSString *)name :(NSString *)type
{
    NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:type]];

    self.movieAsset = [AVAsset assetWithURL:movieURL];
    self.movieItem = [[AVPlayerItem alloc] initWithAsset:self.movieAsset];
    self.moviePlayer = [[AVPlayer alloc] initWithPlayerItem:self.movieItem];
    self.moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    self.movieLayer.videoGravity = AVLayerVideoGravityResize;

    self.movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
    [self.movieLayer setFrame:self.view.frame];

    [self.view.layer addSublayer:self.movieLayer];
    [self.moviePlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

    // Schedule stop after 6 seconds
    [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(stopCurrentMovie:) userInfo:nil repeats:NO];
}

The video is playing, but it doesn't fill (stretch if needed) the entire screen, but it only resizes maintaining its width/height ratio: I have tried all the three values of "videoGravity"... nothing seems to change.

How can I solve? Thank you

Carillon answered 30/1, 2014 at 10:43 Comment(3)
use affineTransform to scale it to fill the screenDiscuss
Isn't there a simpler method? Because that way I have to hard-code scaling options for each supported device/screen. But I'll try anywayCarillon
It is working! But I think I'll opt for two different videos, one for each device screen (3.5 inch and 4 inch).Carillon
R
10

Your setting the Gravity then re-Initing the player try:

- (void)playMovie:(NSString *)name :(NSString *)type
{
 NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:type]];

self.movieAsset = [AVAsset assetWithURL:movieURL];
self.movieItem = [[AVPlayerItem alloc] initWithAsset:self.movieAsset];
self.moviePlayer = [[AVPlayer alloc] initWithPlayerItem:self.movieItem];


self.movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
[self.movieLayer setFrame:self.view.frame];
self.moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.movieLayer.videoGravity = AVLayerVideoGravityResize;

[self.view.layer addSublayer:self.movieLayer];
[self.moviePlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

// Schedule stop after 6 seconds
[NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(stopCurrentMovie:) userInfo:nil repeats:NO];
}
Reactance answered 10/6, 2015 at 14:14 Comment(3)
it is possible to video fit to a viewFrameLugo
AVLayerVideoGravityResizeAspectFill?Reactance
Thanks! you made my day! set videoGravity as AVLayerVideoGravityResizeAspectFill worked for me.Glossographer

© 2022 - 2024 — McMap. All rights reserved.