I\'m trying to copy Apple\'s behavior in video playback that allows the user to stretch the video image to fill the bounds.
@interface FHVideoPlayerView : UI
It was a bug in iOS 5.0 and iOS 5.0.1.
After updating to iOS 5.1 the video gravity changing animation begun to work again.
As voromax pointed out, this is a bug in iOS 5.0. I reverse engineered -[AVPlayerLayer setVideoGravity:]
implementation in iOS 5.1 in order to understand how the animation was supposed to work. Here is how to workaround the bug and have a nice animation on iOS 5.0.
@implementation VideoPlayerView
+ (Class) layerClass
{
return [AVPlayerLayer class];
}
- (AVPlayerLayer *) playerLayer
{
return (AVPlayerLayer *)[self layer];
}
- (NSString *) videoGravity
{
return self.playerLayer.videoGravity;
}
- (void) setVideoGravity:(NSString *)videoGravity
{
self.playerLayer.videoGravity = videoGravity;
// Workaround a bug in iOS 5.0
float avFoundationVersion = [[[NSBundle bundleForClass:[AVPlayerLayer class]] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey] floatValue];
if (avFoundationVersion < 292.24f)
{
@try
{
NSString *contentLayerKeyPath = [NSString stringWithFormat:@"%1$@%2$@.%3$@%2$@", @"player", [@"layer" capitalizedString], @"content"]; // playerLayer.contentLayer
CALayer *contentLayer = [self.playerLayer valueForKeyPath:contentLayerKeyPath];
if ([contentLayer isKindOfClass:[CALayer class]])
[contentLayer addAnimation:[CABasicAnimation animation] forKey:@"sublayerTransform"];
}
@catch (NSException *exception)
{
}
self.bounds = self.bounds;
}
}
@end