8

I'm currently developing a video player for streamed-content using the AVPlayer Framework. I stumbled across the AVPlayerLayer's VideoGravity String-Property which would allow me to set the players Scaling/Resizing Mode to different values.

In order to provide the user with the scaling-features known from the default player, I've set up a method that would execute the following code:

AVPlayerLayer *layer = (AVPlayerLayer *)[self.videoContainer layer];

if([layer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspect])
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
else 
    layer.videoGravity = AVLayerVideoGravityResizeAspect;

This works very well in the Simulator, but somehow not on my iPad 2 with iOS 5.0.1 installed.

Has anyone experienced similar issues? Is this a known iOS Bug with 5.0.1? Is there a better approach of implementing scaling / resizing with AVPlayerLayer?

Any ideas/tips/help and recommendations are greatly appreciated,

Thanks,

Sam

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
samsam
  • 3,125
  • 24
  • 40
  • When you say it doesn't work on a real device, what actually happens? Nothing or some other unexpected behaviour? – reddersky Mar 23 '12 at 11:10
  • unfortunately nothing (visible) happens... if I NSLog(layer.videoGravity) the "correct" VideoGravity String is returned. It also accepts changes to it, but it doesn't really resize the movie-content... – samsam Mar 23 '12 at 13:27
  • I might be barking up the wrong tree here, but why are you setting the videoGravity to a string of the constant name rather than just setting it to the constant itself? – reddersky Mar 23 '12 at 17:57
  • doesn't help either... as mentioned above; the control does take change the value of the videoGravity accordingly, but the playerLayer doesn't do anything (on the pad, on the simulator it works very well) – samsam Mar 26 '12 at 14:58
  • Still ought to set property using the constants directly though rather than the assumed values of those constants :) – reddersky Mar 26 '12 at 15:25
  • of course, i've changed that now :).. – samsam Mar 26 '12 at 15:41

2 Answers2

21

Setting the bounds will internally setNeedsLayout. You must call this your self if you only change the gravity. A call to setNeedsDisplay to force a re-draw couldn't hurt either, although I imagine AVPlayerLayer is updating the layer contents so frequently that it won't matter.

EDIT: Your name is twice as good as mine!

Sam
  • 2,579
  • 17
  • 27
  • 1
    I couldn't get this solution to work for me, and found that the problem was a bug in iOS 5.0 and 5.0.1. If anyone else stumbles on this and needs a solution, check out: http://stackoverflow.com/questions/8918105/animate-avplayerlayer-videogravity-property – ams Aug 09 '12 at 19:45
4

i finally managed to fix my issue by exchanging the above statement with the following...

    if (self.player.status == AVPlayerStatusReadyToPlay) {
    if([((AVPlayerLayer *)[self.videoContainer layer]).videoGravity isEqualToString:AVLayerVideoGravityResizeAspect])
        ((AVPlayerLayer *)[self.videoContainer layer]).videoGravity = AVLayerVideoGravityResizeAspectFill;
    else 
        ((AVPlayerLayer *)[self.videoContainer layer]).videoGravity = AVLayerVideoGravityResizeAspect;

    ((AVPlayerLayer *)[self.videoContainer layer]).bounds = ((AVPlayerLayer *)[self.videoContainer layer]).bounds;
}

setting the AVPlayerLayer's bounds to the AVPlayerLayer's bound seemed to do the trick. although i don't really get why.

however: hurray for working solution.

samsam
  • 3,125
  • 24
  • 40
  • 1
    See also the [Animate AVPlayerLayer videoGravity property](http://stackoverflow.com/a/10489823/21698) question for a solution which animates the video gravity change on iOS 5.0.1. – 0xced Jul 15 '12 at 20:58