我已经按照Apple文档中的建议创建了一个AVPlayerView: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html
所以我将UIView子类添加为我的UIViewController的子视图。
看起来不错,视频效果很好。 但是,当我将视图最小化到一个小窗口时,视频不会随之调整大小。 没关系,当我使用MPMoviePlayerViewController时,我需要使用AVPlayer有几个原因[静音,同时播放多个视频等。]。
这是我的AVPlayerView实现:
@implementation VideoView {
}
+ (Class)layerClass {
return [AVPlayerLayer class];
}
- (AVPlayer*)player {
return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
[(AVPlayerLayer *)[self layer] setPlayer:player];
}
@end
以下是我创建AVPlayer的方法:
AVAsset *asset = [AVURLAsset URLAssetWithURL:Url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
player_vc = [AVPlayer playerWithPlayerItem:anItem];
player_vc.actionAtItemEnd = AVPlayerActionAtItemEndNone;
player_layer = [[VideoView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: player_layer];
[player_layer setPlayer:player_vc];
这里是我用来更改父视图框架的代码:
-(void) minimize {
[UIView animateWithDuration:2.0
animations:^{
self.view.frame = resizeFrame;
} completion:(void (^)(BOOL)) ^{
self.minimized = true;
}
似乎一旦使用特定的框架创建,我就无法将其改为其他任何内容。
您能否告诉我如何使用视图控制器的视图调整AVPlayer图层大小?
由于
答案 0 :(得分:0)
AVPlayerLayers不会自动调整其父视图的大小。如果您要使用AVPlayerLayer覆盖UIView的layerClass
,您还应该覆盖其layoutSublayersOfLayer
方法,如下所示:
- (void)layoutSublayersOfLayer:(CALayer *)layer {
[super layoutSublayersOfLayer:layer];
if (layer == self.layer) {
[self layer].frame = self.bounds;
}
}
在Swift 3中:
class PlayerLayerDisplayer: UIView {
override class var layerClass: AnyClass {
get {
return AVPlayerLayer.self
}
}
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
guard layer == self.layer else {
return
}
layer.frame = self.bounds
}
}