Animate AVPlayerLayer videoGravity property

前端 未结 2 647
借酒劲吻你
借酒劲吻你 2020-12-29 17:41

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         


        
相关标签:
2条回答
  • 2020-12-29 18:28

    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.

    0 讨论(0)
  • 2020-12-29 18:41

    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
    
    0 讨论(0)
提交回复
热议问题