• 视频播放


    {

    //    播放器

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

                

            }

        }

    }

  • 相关阅读:
    在安装SqlServer2008时,有一项安装程序支持规则,为什么重新启动计算机那一项总是失败
    在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
    IIS7上设置MIME让其支持android和Iphone的更新下载
    c# 常用正则
    数据库增容方法
    初识MAC(由window到mac的转变适应)
    无需控件直接导出xls(csv)
    2017中国屏幕分辨率统计---UI设计应注重的问题
    网页制作基础及HTML教学模块安排
    传统教学设计模板
  • 原文地址:https://www.cnblogs.com/yxt9322yxt/p/4769216.html
Copyright © 2020-2023  润新知