用AVPlayer实现视频播放

因为AVPlayer不能直接播放视频,所以要自定义一个UIView作为视频播放层。需要实现以下几个方法,替换CALayer层为AVPlayerLayer作为视频播放层。

#import "PlayView.h"

@implementation PlayView

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

- (AVPlayer *)player {
    return [(AVPlayerLayer *)[self layer] player];
}

- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
    [(AVPlayerLayer *)[self layer] player].usesExternalPlaybackWhileExternalScreenIsActive = YES;
}
@end

控制器中:

#import "PlayerViewController.h"
#import 
#import 
#import "PlayView.h"

@interface PlayerViewController ()

@property (nonatomic,strong) AVPlayer *player;//播放器对象

@property (nonatomic,strong) PlayView *container; //播放器容器
@property (nonatomic,strong) UIButton *playOrPause; //播放/暂停按钮
//@property (weak, nonatomic) IBOutlet UIProgressView *progress;//播放进度
@end

@implementation PlayerViewController

#pragma mark - 控制器视图方法
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setupUI];
    [self.player play];
}

-(void)dealloc{
    [self removeObserverFromPlayerItem:self.player.currentItem];
    [self removeNotification];
}

#pragma mark - 私有方法
-(void)setupUI{
    
    [self.view addSubview:self.container];
    [self.view addSubview:self.playOrPause];
    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.view);
        make.height.mas_equalTo(300);
    }];
    
    [self.playOrPause mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.container).offset(20);
        make.top.equalTo(self.container.mas_bottom);
    }];
    
}

- (void)playOrPauseClick:(UIButton *)btn
{
    NSString *titel;
    if(self.player.rate==0){ //说明时暂停
        titel = @"暂停";
        [self.player play];
    }else if(self.player.rate==1){//正在播放
        [self.player pause];
        titel = @"开始";
    }
    [btn setTitle:titel forState:UIControlStateNormal];

}
/**
 *  截取指定时间的视频缩略图
 *
 *  @param timeBySecond 时间点
 */
/**
 *  根据视频索引取得AVPlayerItem对象
 *
 *  @param videoIndex 视频顺序索引
 *
 *  @return AVPlayerItem对象
 */
-(AVPlayerItem *)getPlayItem:(int)videoIndex{
//    NSString *urlStr= [NSString stringWithFormat:@"http://dl16.80s.im:920/1511/[TinasheChris.Brown]Player/[TinasheChris.Brown]Player_hd.mp4"];
//    NSURL *url=[NSURL URLWithString:urlStr];
    NSString *urlStr= [[NSBundle mainBundle] pathForResource:@"3" ofType:@"mp4"];
    NSURL *url=[NSURL fileURLWithPath:urlStr];
    AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
    return playerItem;
}
/**
 *  初始化播放器
 *
 *  @return 播放器对象
 */
-(AVPlayer *)player{
    if (!_player) {
        AVPlayerItem *playerItem=[self getPlayItem:0];
        _player=[AVPlayer playerWithPlayerItem:playerItem];
        //[self addProgressObserver];
        [self addObserverToPlayerItem:playerItem];
    }
    return _player;
}

- (PlayView *)container{
    if (!_container) {
        _container = [[PlayView alloc]init];
        [_container setPlayer:self.player];
    }
    return _container;
}

- (UIButton *)playOrPause
{
    if (!_playOrPause) {
        _playOrPause = [[UIButton alloc] init];
        [_playOrPause setTitle:@"暂停" forState:UIControlStateNormal];
        [_playOrPause setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_playOrPause addTarget:self action:@selector(playOrPauseClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playOrPause;
}

你可能感兴趣的:(用AVPlayer实现视频播放)