首页 > iOS AVPlayer播放视频时,视频所在的View始终会在前面

iOS AVPlayer播放视频时,视频所在的View始终会在前面

我把AVPlayerLayer添加到了自定义的containerViewlayer上面,视频播放正常,但是我想在视频播放时,添加一些东西,所以我在containerView上面添加了一个coverView,然后在coverView上面添加了一些子控件,用于在视频播放时显示相关信息,我添加的顺序是这样的:

[self.view addSubview:_containerView];
[_containerView addSubview:_coverView];

然后我发现,视频始终在前面,coverView根本挡不住,我又试了

[_containerView bringSubviewToFront:_coverView];

coverView也挡不住视频,哪位大神帮我看一下。


不知到这个问题解决了吗?
AVPlayerLayer一定要在_coverView之前加入_containterView, 比如:

[_containerView.layer addSubLayer:avplayerLayer];
[_containerView addSubView:_coverView];

[结果截图]

[测试代码]

//
//  ViewController.m
//  HelloAVPlayer
//
//  Created by Jeffrey Zhang on 16/3/25.
//  Copyright © 2016年 Jeffrey Zhang. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
@property (nonatomic, strong) UIView* containerView;
@property (nonatomic, strong) UIView* coverView;
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 300)];
    _containerView.userInteractionEnabled = YES;
    _containerView.backgroundColor = [[UIColor alloc] initWithRed:0 green:0 blue:100 alpha:0.2];
    [self.view addSubview:_containerView];
    
    _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width/2, 40)];
    _coverView.userInteractionEnabled = YES;
    _coverView.backgroundColor = [[UIColor alloc] initWithRed:0 green:255 blue:0 alpha:1];
    
    UIButton* btnPause = [[UIButton alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width/2,40)];
    [btnPause addTarget:self action:@selector(actionLogin:) forControlEvents:UIControlEventTouchUpInside];
    [btnPause setTitle:@"PAUSE" forState:UIControlStateNormal];
    //btnPause.translatesAutoresizingMaskIntoConstraints = NO;
    [_coverView addSubview:btnPause];
    
    
    NSLog(@"hello, AVPlayer");
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"screencapture" ofType:@"mov"];
    //NSLog(@"%@",filePath);
    NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
    //NSLog(@"%@",sourceMovieURL);
    AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
    _player = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    playerLayer.frame = _containerView.layer.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    
    //[_containerView bringSubviewToFront:_coverView];
    
    [_containerView.layer addSublayer:playerLayer];
    [_containerView addSubview:_coverView];
    
}
-(IBAction)actionLogin:(id)sender
{
    NSLog(@"PAUSE");
}
-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)btnPlay_OnClick:(id)sender {

    [_player play];
}


@end
【热门文章】
【热门文章】