iOS之AVPlayer的简单应用

导入头文件

#import 
#import 

声明播放控件属性

@property (nonatomic, strong)AVPlayer *player;

声明滑动条

@property (nonatomic, strong)UISlider *slider;

声明播放的总时间长

@property (nonatomic,assign)CGFloat sumPlayOperation;

//设置播放的url
    NSURL *url = [NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
  //设置播放的项目
      AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:url];
//初始化player对象
self.player = [[AVPlayer alloc]initWithPlayerItem:item];
 //设置播放页面
 //在开发中,单纯使用AVPlayer类是无法显示视频的,要将视频层添加至AVPlayerLayer中,这样才能将视频显示出来 所以设置AVPlayerLayer播放页面
AVPlayerLayer *layout = [AVPlayerLayer playerLayerWithPlayer:_player];
 layout.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300);
      layout.backgroundColor = [UIColor cyanColor].CGColor;
    //设置播放窗口和当前视图之间的比例显示内容
      layout.videoGravity = AVLayerVideoGravityResizeAspect;
 [self.view.layer addSublayer:layout];
      self.player.volume = 1.0f;
//代码块
      [self.playView play:^(UIButton *play) {
            [self.player play];
      } pause:^(UIButton *pause) {
            [self.player pause];
      } last:^(UIButton *last) {
            NSLog(@"上一首");
      } next:^(UIButton *next) {
            NSLog(@"下一首");
      }];
//创建slider
_slider = [[UISlider alloc]initWithFrame:CGRectMake(self.view.center.x - 100, 280, 200, 30)];
      [self.view addSubview:_slider];
      _slider.value = 0.0f;
      [_slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventTouchUpInside];

//改变进度
- (void)handleSlider:(UISlider *)slider {
      self.sumPlayOperation = self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
      //CMTimeMake(a,b) a表示当前时间,b表示每秒钟有多少帧
      [self.player seekToTime:CMTimeMakeWithSeconds(self.slider.value * self.sumPlayOperation, self.player.currentItem.duration.timescale) completionHandler:^(BOOL finished) {
            [self.player play];
      }];
}

大致这样


iOS之AVPlayer的简单应用_第1张图片
QQ20160820-0.png

你可能感兴趣的:(iOS之AVPlayer的简单应用)