实用技术首页投稿(暂停使用,暂停投稿)iOS开发资料收集区

IOS视频播放AVPlayer(一)

2016-07-18  本文已影响1012人  禾子_____
AVPlayerDemo.gif

简单写的一个视频播放器,可以播放、暂停、拖放、屏幕旋转(横屏全屏)、播放时隐藏状态栏等基本功能...

关键代码:

初始化播放器

#import <AVFoundation/AVFoundation.h>
//播放器相关
@property (strong,nonatomic) AVPlayerItem *playerItem;
@property (strong,nonatomic) AVPlayer *player;
@property (strong,nonatomic) AVAsset *asset;
@property (strong,nonatomic) AVPlayerLayer *playerLayer;
//UI相关
@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; //进度条
@property (weak, nonatomic) IBOutlet UIButton *playBtn; //播放按钮
@property (weak, nonatomic) IBOutlet UILabel *timeLabel; //视频总时长Label
@property (strong,nonatomic) NSTimer *timer;  
@property (weak, nonatomic) IBOutlet UIButton *BigScreenBtn; //全屏按钮
@property (weak, nonatomic) IBOutlet UILabel *runTimeLabel; //视频当前时间Label
@property (assign,nonatomic) BOOL isPlay; //是否在播放(控制进度条是否移动)

//创建播放器
    self.asset = [AVAsset assetWithURL:[NSURL URLWithString:_urlString]];
    
    self.playerItem = [AVPlayerItem playerItemWithAsset:self.asset];
//添加监听播放源状态.在销毁时要移除
    [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];

初始化进度条

//获取进度条信息
    CMTime cmtime = self.asset.duration;
    int seconds = (int)cmtime.value/cmtime.timescale;//视频的时长/视频压缩比
    self.slider.maximumValue = seconds;//设置slide的最大值为换算后的总时间值
    self.slider.value = 0;//设置slide的初始值为0
    //视频总时间 以 mm:ss 的格式显示在lable中
    self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",seconds/60/60,seconds/60,seconds%60];
    //初始化播放进度 为 00:00
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshSlideTime) userInfo:nil repeats:YES];

进度条移动

//刷新进度时间,进度条
-(void)refreshSlideTime{
    
    //获取当前视频的播放时长,根据当前的压缩比转换后, 以mm:ss 格式显示在label中
    if (self.isPlay) {
        //获取进度条信息
        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;
        //当视频结束时,停止定时器并将标志位置为 NO,以便点击play按钮时,可以直接播放视频,但是要注意在slide的事件下处理定时器
        if (self.slider.value == self.playerItem.duration.value/self.playerItem.duration.timescale){
            [self.playBtn setSelected:NO];
            [self.timer invalidate];
            
        }

    }
    
}

拖放进度条,改变播放位置

//拖放进度条
- (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]; //跳到当前时间开始播放
        [self.player play];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshSlideTime) userInfo:nil repeats:YES];
    }else{
        [self.player pause];
    }
 
}

备注: 隐藏显示状态栏需要在plist文件里添加

Snip20160718_1.png
[附上Demo] (http://www.code4app.com/thread-9501-1-1.html)
上一篇下一篇

猜你喜欢

热点阅读