IOS視頻播放AVPlayer(一)

AVPlayerDemo.gif

簡(jiǎn)單寫的一個(gè)視頻播放器,可以播放、暫停、拖放、屏幕旋轉(zhuǎn)(橫屏全屏)、播放時(shí)隱藏狀態(tài)欄等基本功能...

關(guān)鍵代碼:

初始化播放器

#import <AVFoundation/AVFoundation.h>
//播放器相關(guān)
@property (strong,nonatomic) AVPlayerItem *playerItem;
@property (strong,nonatomic) AVPlayer *player;
@property (strong,nonatomic) AVAsset *asset;
@property (strong,nonatomic) AVPlayerLayer *playerLayer;
//UI相關(guān)
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *footerBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerTop;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *videoTitleLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider; //進(jìn)度條
@property (weak, nonatomic) IBOutlet UIButton *playBtn; //播放按鈕
@property (weak, nonatomic) IBOutlet UILabel *timeLabel; //視頻總時(shí)長(zhǎng)Label
@property (strong,nonatomic) NSTimer *timer;  
@property (weak, nonatomic) IBOutlet UIButton *BigScreenBtn; //全屏按鈕
@property (weak, nonatomic) IBOutlet UILabel *runTimeLabel; //視頻當(dāng)前時(shí)間Label
@property (assign,nonatomic) BOOL isPlay; //是否在播放(控制進(jìn)度條是否移動(dòng))

//創(chuàng)建播放器
    self.asset = [AVAsset assetWithURL:[NSURL URLWithString:_urlString]];
    
    self.playerItem = [AVPlayerItem playerItemWithAsset:self.asset];
//添加監(jiān)聽播放源狀態(tài).在銷毀時(shí)要移除
    [self.playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.backgroundColor = [UIColor blackColor].CGColor;
    self.playerLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.view.layer addSublayer:self.playerLayer];

初始化進(jìn)度條

//獲取進(jìn)度條信息
    CMTime cmtime = self.asset.duration;
    int seconds = (int)cmtime.value/cmtime.timescale;//視頻的時(shí)長(zhǎng)/視頻壓縮比
    self.slider.maximumValue = seconds;//設(shè)置slide的最大值為換算后的總時(shí)間值
    self.slider.value = 0;//設(shè)置slide的初始值為0
    //視頻總時(shí)間 以 mm:ss 的格式顯示在lable中
    self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",seconds/60/60,seconds/60,seconds%60];
    //初始化播放進(jìn)度 為 00:00
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshSlideTime) userInfo:nil repeats:YES];

進(jìn)度條移動(dòng)

//刷新進(jìn)度時(shí)間,進(jìn)度條
-(void)refreshSlideTime{
    
    //獲取當(dāng)前視頻的播放時(shí)長(zhǎng),根據(jù)當(dāng)前的壓縮比轉(zhuǎn)換后, 以mm:ss 格式顯示在label中
    if (self.isPlay) {
        //獲取進(jìn)度條信息
        double time = self.player.currentTime.value / self.player.currentTime.timescale;
        self.slider.value = time;
        NSString *runTimeStr = [NSString stringWithFormat:@"%02d:%02d:%02d",(int)time/60/60,(int)time/60,(int)time%60];
        self.runTimeLabel.text = runTimeStr;
        //當(dāng)視頻結(jié)束時(shí),停止定時(shí)器并將標(biāo)志位置為 NO,以便點(diǎn)擊play按鈕時(shí),可以直接播放視頻,但是要注意在slide的事件下處理定時(shí)器
        if (self.slider.value == self.playerItem.duration.value/self.playerItem.duration.timescale){
            [self.playBtn setSelected:NO];
            [self.timer invalidate];
            
        }

    }
    
}

拖放進(jìn)度條,改變播放位置

//拖放進(jìn)度條
- (IBAction)sliderValueChange{
    
    if (self.slider.value <= self.slider.maximumValue){
        
        CMTime moveTime = CMTimeMake(self.player.currentTime.timescale*self.slider.value, self.player.currentTime.timescale);
        [self.player seekToTime:moveTime]; //跳到當(dāng)前時(shí)間開始播放
        [self.player play];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshSlideTime) userInfo:nil repeats:YES];
    }else{
        [self.player pause];
    }
 
}

備注: 隱藏顯示狀態(tài)欄需要在plist文件里添加

Snip20160718_1.png

[附上Demo] (http://www.code4app.com/thread-9501-1-1.html)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容