AVPlayerLayer没有绑定到UIView框架

时间:2016-02-16 21:31:39

标签: iphone xcode swift avplayerlayer cglayer

我试图将AVPlayerLayer添加到UIView

YYYYYYYY

这是视频的容器(蓝色):

This is the view:

约束: Constraints:

最终结果: enter image description here

我无法弄清楚为什么视频没有绑定到UIVIew坐标。如果我把它绑定到控制器的超级视图中,那就没关系。

4 个答案:

答案 0 :(得分:23)

当视图的帧发生变化时,子图层不会自动调整大小,您必须手动完成。您可以将UIView子类化以使其更容易。

class VideoContainerView: UIView {
  var playerLayer: CALayer?
  override func layoutSublayersOfLayer(layer: CALayer) {
    super.layoutSublayersOfLayer(layer)
    playerLayer?.frame = self.bounds
  }
}

然后在你的代码中:

self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame =  ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
self.ctrVideo.playerLayer = playerLayer;
player.play();

请确保将“ctrVideo”的类从UIView更改为VideoContainerView。

答案 1 :(得分:0)

我在viewDidLoad事件中添加了AVPlayerLayer ...在应用约束之前。

答案 2 :(得分:-1)

这是AVPlayerLayer支持的视图,它试图保持其宽高比。

PlayerView.h:

@interface PlayerView : UIView

@property (strong, nonatomic) AVPlayerLayer *layer;

@end

PlayerView.m:

@interface PlayerView ()

@property (strong, nonatomic) NSLayoutConstraint *aspectConstraint;

@end

@implementation PlayerView

@dynamic layer;

+ (Class)layerClass {
    return [AVPlayerLayer class];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.userInteractionEnabled = NO;

        [self addObserver:self forKeyPath:@"layer.videoRect" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"layer.videoRect"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"layer.videoRect"]) {
        CGSize size = [change[NSKeyValueChangeNewKey] CGRectValue].size;
        if (change[NSKeyValueChangeNewKey] && size.width && size.height) {
            self.aspectConstraint.active = NO;
            self.aspectConstraint = [self.widthAnchor constraintEqualToAnchor:self.heightAnchor multiplier:size.width / size.height];
            self.aspectConstraint.priority = UILayoutPriorityDefaultHigh;
            self.aspectConstraint.active = YES;
        }
        else {
            self.aspectConstraint.active = NO;
        }
    }
}

@end

答案 3 :(得分:-1)

这是@almas在 Swift 4

中的精彩答案
class VideoContainerView: UIView {    
    var playerLayer: CALayer?

    override func layoutSublayers(of layer: CALayer) {
        super.layoutSublayers(of: layer)
        playerLayer?.frame = self.bounds
    }
}