美文网首页
iOS之AVPlayer的简单应用

iOS之AVPlayer的简单应用

作者: 高压玩笑 | 来源:发表于2016-08-20 09:51 被阅读2680次

导入头文件

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

声明播放控件属性

@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];
      }];
}

大致这样


QQ20160820-0.png

相关文章

网友评论

      本文标题:iOS之AVPlayer的简单应用

      本文链接:https://www.haomeiwen.com/subject/fbcisttx.html