Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretch video on AVPlayerLayer

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

like image 933
Umar Jamil Avatar asked Jan 30 '14 10:01

Umar Jamil


1 Answers

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];
}
like image 200
Sean Lintern Avatar answered Nov 12 '22 20:11

Sean Lintern