当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS流媒体开发:滑动手势控制音量、亮度和进度

iOS流媒体开发:滑动手势控制音量、亮度和进度

2018年12月26日  | 移动技术网IT编程  | 我要评论

邦德克尤,樱桃红了第二部,45iii

自定义一个uibutton
我们需要在视频上添加一个透明的button来捕捉和响应用户的点击滑动屏幕的事件,同时在.h文件中声明一个代理,向相应的页面传递自定义button的响应事件,代码如下:

.h文件

    #import 

    @protocol wbybuttondelegate <nsobject>

    /**
     * 开始触摸
     */
    - (void)touchesbeganwithpoint:(cgpoint)point;

    /**
     * 结束触摸
     */
    - (void)touchesendwithpoint:(cgpoint)point;

    /**
     * 移动手指
     */
    - (void)touchesmovewithpoint:(cgpoint)point;

    @end

    @interface zylbutton : uibutton

    /**
     * 传递点击事件的代理
     */
    @property (weak, nonatomic) id <wbybuttondelegate> touchdelegate;

    @end
.m文件

    #import "wbybutton.h"

    @implementation wbybutton


    //触摸开始
    - (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event {
        [super touchesbegan:touches withevent:event];
        //获取触摸开始的坐标
        uitouch *touch = [touches anyobject];
        cgpoint currentp = [touch locationinview:self];
        [self.touchdelegate touchesbeganwithpoint:currentp];
    }

    //触摸结束
    - (void)touchesended:(nsset<uitouch *> *)touches withevent:(uievent *)event {
        [super touchesended:touches withevent:event];
        uitouch *touch = [touches anyobject];
        cgpoint currentp = [touch locationinview:self];
        [self.touchdelegate touchesendwithpoint:currentp];
    }

    //移动
    - (void)touchesmoved:(nsset<uitouch *> *)touches withevent:(nullable uievent *)event {
        uitouch *touch = [touches anyobject];
        cgpoint currentp = [touch locationinview:self];
        [self.touchdelegate touchesmovewithpoint:currentp];
    }

    @end

注意:之所以选择uibutton有2点原因:1、不用手动开启userinteractionenabled用户交互2、同时可以很方便的为uibutton添加target,不需要像uiview那样在再定义一个uitapgesturerecognizer,当然uibutton添加各种状态的背景颜色各背景图也要比uiview方便得多。

将自定义的button添加到视频上

  //添加自定义的button到视频画面上
      self.button = [[wbybutton alloc] initwithframe:playerlayer.frame];
      self.button.touchdelegate = self;
      [playerview addsubview:self.button];

在代理方法中改变定音量、亮度和进度
首先定义个一枚举表示上下左右,这里只需要判断手指是上下还是左右滑动

  typedef ns_enum(nsuinteger, direction) {
      directionleftorright,
      directionupordown,
      directionnone
  };

同时声明一个表示方向的变量、一个记录用户触摸视频时的坐标变量、一个记录用户触摸视频时的亮度和音量大小的变量和一个记录用户触摸屏幕是视频进度的变量

  @property (assign, nonatomic) direction direction;
  @property (assign, nonatomic) cgpoint startpoint;
  @property (assign, nonatomic) cgfloat startvb;
  @property (assign, nonatomic) cgfloat startvideorate;

刚开始触摸视频的代理
当用户首次触摸视频时,记录首次触摸的坐标、当前音量或者亮度、当前视频的进度,为了获取当前音量要首先定义mpvolumeview、 uislider

  @property (strong, nonatomic) mpvolumeview *volumeview;//控制音量的view

  @property (strong, nonatomic) uislider* volumeviewslider;//控制音量
    //设置self.volumeview的frame
    self.volumeview.frame = cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 9.0 / 16.0);
在getter方法中初始化:
    - (mpvolumeview *)volumeview {
        if (_volumeview == nil) {
            _volumeview  = [[mpvolumeview alloc] init];
            [_volumeview sizetofit];
            for (uiview *view in [_volumeview subviews]){
                if ([view.class.description isequaltostring:@"mpvolumeslider"]){
                    self.volumeviewslider = (uislider*)view;
                    break;
                }
            }
        }
        return _volumeview;
    }

然后在开始触摸的代理里记录

  #pragma mark - 开始触摸

    - (void)touchesbeganwithpoint:(cgpoint)point {
        //记录首次触摸坐标
        self.startpoint = point;
        //检测用户是触摸屏幕的左边还是右边,以此判断用户是要调节音量还是亮度,左边是亮度,右边是音量
        if (self.startpoint.x <= self.button.frame.size.width / 2.0) {
            //亮度
            self.startvb = [uiscreen mainscreen].brightness;
        } else {
            //音量
            self.startvb = self.volumeviewslider.value;
        }
    }
    cmtime ctime = self.avplayer.currenttime;
    self.startvideorate = ctime.value / ctime.timescale / self.total;

接着在拖动的代理方法里改变音量、亮度和进度

 #pragma mark - 拖动 
  - (void)touchesmovewithpoint:(cgpoint)point {
      //得出手指在button上移动的距离
      cgpoint panpoint = cgpointmake(point.x - self.startpoint.x, point.y - self.startpoint.y);
      //分析出用户滑动的方向
       if (self.direction == directionnone) {
          if (panpoint.x >= 30 || panpoint.x <= -30) {
              //进度
              self.direction = directionleftorright;
          } else if (panpoint.y >= 30 || panpoint.y <= -30) {
              //音量和亮度
              self.direction = directionupordown;
          }
      }

      if (self.direction == directionnone) {
          return;
      } else if (self.direction == directionupordown) {
          //音量和亮度
          if (self.startpoint.x <= self.button.frame.size.width / 2.0) {
      //调节亮度
              if (panpoint.y < 0) {
                  //增加亮度
                  [[uiscreen mainscreen] setbrightness:self.startvb + (-panpoint.y / 30.0 / 10)];
              } else {
                  //减少亮度
                  [[uiscreen mainscreen] setbrightness:self.startvb - (panpoint.y / 30.0 / 10)];
              }

          } else {
              //音量
              if (panpoint.y < 0) {
                   //增大音量
                  [self.volumeviewslider setvalue:self.startvb + (-panpoint.y / 30.0 / 10) animated:yes];
                  if (self.startvb + (-panpoint.y / 30 / 10) - self.volumeviewslider.value >= 0.1) {
                      [self.volumeviewslider setvalue:0.1 animated:no];
                      [self.volumeviewslider setvalue:self.startvb + (-panpoint.y / 30.0 / 10) animated:yes];
                  }

              } else {
                  //减少音量
                  [self.volumeviewslider setvalue:self.startvb - (panpoint.y / 30.0 / 10) animated:yes];
              }
          }
      } else if (self.direction == directionleftorright ) {
          //进度
          cgfloat rate = self.startvideorate + (panpoint.x / 30.0 / 20.0);
          if (rate > 1) {
              rate = 1;
          } else if (rate < 0) {
              rate = 0;
          }
      }
}

注意:1、前面提到一个增大音量的bug,我这里的解决办法是如果检测到用户设置的音量比当前的音量大于0.1,表示此时设置的音量已经无效了,然后把音量设置为0.1后再设置为我们设置无效的那个音量就可以了,具体做法参考代码;2、在修改视频播放进度的时候,最好不在移动的方法中实时修改视频播放进度,一方面会造成卡顿的现象,一方面没有必要这么做,所以这里只是记录了用户想要调整的进度,然后在触摸结束的方法中设置进度;3、这改变音量会调用系统自己的ui显示音量大小,但是在设置亮度的时候是系统没有提供相应的ui,需要我们自己设置,这个小伙伴们按照项目的需求自行添加一个ui效果就好了。

触摸结束的代理
#pragma mark - 结束触摸
  - (void)touchesendwithpoint:(cgpoint)point {
      if (self.direction == directionleftorright) {
          [self.avplayer seektotime:cmtimemakewithseconds(self.total * self.currentrate, 1) completionhandler:^(bool finished) {
              //在这里处理进度设置成功后的事情
          }];
      }
  }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网