• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
那一季&花开
博客园    首页    新随笔    联系   管理    订阅  订阅

视频播放

{

//    播放器

    AVPlayer *player;

//    承载视图

    UIView *contentView;

//    控件承载视图

    UIView *controlView;

//  进度条

    UISlider *progressSlider;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*9/16.f)];

    

    contentView.backgroundColor = [UIColor blackColor];

    

    

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

    

    [contentView addGestureRecognizer:pan];

    

    [self.view addSubview:contentView];

    

    

    

    

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"abc" ofType:@"mp4"]];

 

    //    通过URL创建

    player = [[AVPlayer alloc] initWithURL:url];

    //    AVPlayer 需要生成一个Layer 图层,添加到view的layer

    AVPlayerLayer *layer =[AVPlayerLayer playerLayerWithPlayer:player];

    layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*9/16.f);

    [contentView.layer addSublayer:layer];

    

    [player play];

    

    

//    控制视图

    controlView = [[UIView alloc] initWithFrame:CGRectMake(0,contentView.frame.size.height-50 ,  kScreenWidth, 50)];

    [controlView setBackgroundColor:[UIColor clearColor]];

    [contentView addSubview:controlView];

    

    

//    添加半透明遮罩

    UIView *maskView = [[UIView alloc] initWithFrame:controlView.bounds];

    maskView.backgroundColor = [UIColor blackColor];

    maskView.alpha = .8;

    [controlView addSubview:maskView];

    

 

//    按钮

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    [btn setFrame:CGRectMake(0,0, 50, 50)];

    [btn setTitle:@"▶️" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(playOrPause:) forControlEvents:UIControlEventTouchUpInside];

    

    [controlView addSubview:btn];

    

    

//进度条

    progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(70, 0, kScreenWidth-140, 50)];

    

    progressSlider.value = 0;

 

    [progressSlider addTarget:self action:@selector(sliderAct:) forControlEvents:UIControlEventValueChanged];

    [progressSlider addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchDown];

    [progressSlider addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];

 

    

    [controlView addSubview:progressSlider];

    

    

    

//    进度条跟着视频播放时间 自动滑动

    

    __weak id weakSelf = self;

    

    [player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.2f, 24) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {

        

        ViewController *strongSelf = weakSelf;

        

        //视频第一次回调 设置slider的长度

        if (time.value/time.timescale == 0) {

            

            CMTime duration = strongSelf->player.currentItem.duration;

            long second = duration.value/duration.timescale;

            strongSelf->progressSlider.maximumValue = second;

 

        }

        

        

        //更新slider的value

        long second = time.value/time.timescale;

        strongSelf->progressSlider.value = second;

        

    }];

    

    

 

    

}

 

 

 

#pragma mark -

#pragma mark - UIActions

//手势触发 方法

-(void)panAction:(UIPanGestureRecognizer*)ges{

    

    CGPoint location = [ges locationInView:contentView];

    

    //        右边控制音量

    if( location.x>kScreenWidth/2){

    

        CGPoint translation = [ges translationInView:contentView];

        

        float change = translation.y/contentView.frame.size.height;

        

        if (translation.y) {

         

            [self getSystemVolumeAdd:change];

            

        }

 

    }else{

    //    左边控制亮度

 

        CGPoint translation = [ges translationInView:contentView];

        

        float change = translation.y/contentView.frame.size.height;

        

        [UIScreen mainScreen].brightness-= change;

        

    

    }

    

    

 

}

 

 

//AVPlayer播放或者暂停 可以调用play方法,也可以直接设置速度 rate

-(void)play{

    player.rate = 1;

}

 

-(void)pause{

    player.rate = 0;

}

 

 

 

-(void)sliderAct:(UISlider *)slider{

    

    int second = slider.value;

//    跳转到某个时间点

    [player seekToTime:CMTimeMakeWithSeconds(second, 24)];

}

 

 

 

 

-(void)playOrPause:(UIButton*)btn{

    

    if (player.rate == 0) {

        

        player.rate = 1.0f;

        

    }else{

        player.rate = 0.0f;

    }

    

    

 

}

 

 

//获取声音大小

//创建MPVolumeView ,然后遍历子视图,获取到MPVolumeSlider,

//获得系统当前声音大小 使用            volume = [(UISlider*)v value];

//设置系统声音 用setValue: animated:

//0~1范围

-(void)getSystemVolumeAdd:(float) f{

    

    MPVolumeView *volumeView = [[MPVolumeView alloc] init];

    

    float volume = 0.0;

    

    for (UIView *v in volumeView.subviews) {

        

        if ([v isKindOfClass:NSClassFromString(@"MPVolumeSlider")]) {

            

            

 

            volume = [(UISlider*)v value];

            

            [(UISlider*)v setValue:volume-f animated:YES];

 

            

        }

 

    }

 

}

 

posted @ 2015-08-29 15:31  那一季&花开  阅读(177)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3