用AVPlayer實(shí)現(xiàn)視頻播放

因?yàn)锳VPlayer不能直接播放視頻,所以要自定義一個(gè)UIView作為視頻播放層。需要實(shí)現(xiàn)以下幾個(gè)方法,替換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 <AVFoundation/AVFoundation.h>
#import <Masonry/Masonry.h>
#import "PlayView.h"

@interface PlayerViewController ()

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

@property (nonatomic,strong) PlayView *container; //播放器容器
@property (nonatomic,strong) UIButton *playOrPause; //播放/暫停按鈕
//@property (weak, nonatomic) IBOutlet UIProgressView *progress;//播放進(jìn)度
@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){ //說明時(shí)暫停
        titel = @"暫停";
        [self.player play];
    }else if(self.player.rate==1){//正在播放
        [self.player pause];
        titel = @"開始";
    }
    [btn setTitle:titel forState:UIControlStateNormal];

}
/**
 *  截取指定時(shí)間的視頻縮略圖
 *
 *  @param timeBySecond 時(shí)間點(diǎn)
 */
/**
 *  根據(jù)視頻索引取得AVPlayerItem對(duì)象
 *
 *  @param videoIndex 視頻順序索引
 *
 *  @return AVPlayerItem對(duì)象
 */
-(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 播放器對(duì)象
 */
-(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容