码迷,mamicode.com
首页 > 其他好文 > 详细

视频播放

时间:2015-08-29 16:39:46      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

{

//    播放器

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

 

            

        }

 

    }

 

}

 

视频播放

标签:

原文地址:http://www.cnblogs.com/yxt9322yxt/p/4769216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!