android 开发ios开发AVPlayer

[iOS]仿抖音 火山视频滑动播放 UIScrollView 自

2017-10-09  本文已影响2774人  阳眼的熊

简概:

Demo滑动效果

解决方案一

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if(self)
  {
    self.contentSize = CGSizeMake(0, frame.size.height * 3);
    self.contentOffset = CGPointMake(0, frame.size.height);
    self.pagingEnabled = YES;
    self.opaque = YES;
    self.backgroundColor = [UIColor clearColor];
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.delegate = self;
    
    self.upperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    self.middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, frame.size.height)];
    self.downImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height*2, frame.size.width, frame.size.height)];
    [self addSubview:self.upperImageView];
    [self addSubview:self.middleImageView];
    [self addSubview:self.downImageView];
    
  }
  return self;
}

-(void) initPlayer
{
  KSYMoviePlayerController *_player;
  _player = [[KSYMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:self.live.VideoAddress]];
  _player.view.backgroundColor = [UIColor clearColor];
  [self.player setBufferSizeMax:1];
  _player.view.autoresizesSubviews = true;
  [_player.view setFrame: self.view.bounds];  // player's frame must match parent's
  _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  _player.shouldAutoplay = TRUE;
  _player.shouldLoop = YES;
  _player.scalingMode = MPMovieScalingModeAspectFit;
  [_player prepareToPlay];
 
  self.player = _player;
  self.player.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
  self.player.shouldAutoplay = YES;
  self.view.autoresizesSubviews = YES;
  [self.playerScrollView addSubview:self.player.view];
  
  [[NSNotificationCenter defaultCenter]addObserver:self
                                          selector:@selector(handlePlayerNotify:)
                                              name:(MPMoviePlayerFirstVideoFrameRenderedNotification)
                                            object:nil];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  [self switchPlayer:scrollView];
}

- (void)switchPlayer:(UIScrollView*)scrollView
{
  CGFloat offset = scrollView.contentOffset.y;
  if (self.lives.count) {
    if (offset >= 2*self.frame.size.height)
    {
      // slides to the down player
      scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
      _currentIndex++;
      self.upperImageView.image = self.middleImageView.image;
      self.middleImageView.image = self.downImageView.image;
      if (_currentIndex == self.lives.count - 1)
      {
        _downLive = [self.lives firstObject];
      } else if (_currentIndex == self.lives.count)
      {
        _downLive = self.lives[1];
        _currentIndex = 0;
        
      } else
      {
        _downLive = self.lives[_currentIndex+1];
      }
      [self prepareForImageView:self.downImageView withLive:_downLive];
      if (_previousIndex == _currentIndex) {
        return;
      }
      if ([self.playerDelegate respondsToSelector:@selector(playerScrollView:currentPlayerIndex:)]) {
        [self.playerDelegate playerScrollView:self currentPlayerIndex:_currentIndex];
        _previousIndex = _currentIndex;
        NSLog(@"current index in delegate: %ld -%s",_currentIndex,__FUNCTION__);
      }
    }
    else if (offset <= 0)
    {
      // slides to the upper player
      scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
      _currentIndex--;
      self.downImageView.image = self.middleImageView.image;
      self.middleImageView.image = self.upperImageView.image;
      if (_currentIndex == 0)
      {
        _upperLive = [self.lives lastObject];
        
      } else if (_currentIndex == -1)
      {
        _upperLive = self.lives[self.lives.count - 2];
        _currentIndex = self.lives.count-1;
        
      } else
      {
        _upperLive = self.lives[_currentIndex - 1];
      }
      [self prepareForImageView:self.upperImageView withLive:_upperLive];
      if (_previousIndex == _currentIndex) {
        return;
      }
      if ([self.playerDelegate respondsToSelector:@selector(playerScrollView:currentPlayerIndex:)]) {
        [self.playerDelegate playerScrollView:self currentPlayerIndex:_currentIndex];
        _previousIndex = _currentIndex;
        NSLog(@"current index in delegate: %ld -%s",_currentIndex,__FUNCTION__);
      }
    }
  }
}

- (void) prepareForImageView: (UIImageView *)imageView withLive:(VideoInfoModel *)live
{
  [imageView sd_setImageWithURL:[NSURL URLWithString:live.coverImageAddress]];
}

#pragma mark DYPlayerScrollViewDelegate

- (void)playerScrollView:(DYPlayerScrollView *)playerScrollView currentPlayerIndex:(NSInteger)index
{
  NSLog(@"current index from delegate:%ld  %s",(long)index,__FUNCTION__);
  if (self.index == index) {
    return;
  } else {
    [self reloadPlayerWithLive:self.dataList[index]];
    
    self.index = index;
  }
}

- (void)reloadPlayerWithLive:(VideoInfoModel *)live
{
  [self.player reset:false];
  [self.player.view setHidden:true];
  [self.player setUrl:[NSURL URLWithString:live.VideoAddress]];
  [self.player setShouldAutoplay:YES];
  [self.player setBufferSizeMax:1];
  [self.player setShouldLoop:YES];
  self.player.view.backgroundColor = [UIColor clearColor];
  [self.player prepareToPlay];
}

解决方案二

@property (nonatomic, strong) KSYMoviePlayerController *upPerPlayer, *middlePlayer, *downPlayer;

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if(self)
  {
// 在DYPlayerScrollView init方法中分别初始化 upperImageView middleImageView downImageView 
以及 upPerPlayer middlePlayer downPlayer
  }
}
- (void)switchPlayer:(UIScrollView*)scrollView
{
  CGFloat offset = scrollView.contentOffset.y;
  if (self.lives.count) {
    if (offset >= 2*self.frame.size.height)
    {
      // slides to the down player
      scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
      _currentIndex++;
      self.upperImageView.image = self.middleImageView.image;
      self.middleImageView.image = self.downImageView.image;

      if (self.upPerPlayer.view.frame.origin.y == 0) {
        self.upPerPlayer.view.frame = CGRectMake(0, SCREEN_HEIGHT * 2, SCREEN_WIDTH, SCREEN_HEIGHT);
      }else{
        self.upPerPlayer.view.frame = CGRectMake(0, self.upPerPlayer.view.frame.origin.y - SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
      }
      
      if (self.middlePlayer.view.frame.origin.y == 0) {
        self.middlePlayer.view.frame = CGRectMake(0, SCREEN_HEIGHT * 2, SCREEN_WIDTH, SCREEN_HEIGHT);
      }else{
        self.middlePlayer.view.frame = CGRectMake(0, self.middlePlayer.view.frame.origin.y - SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
      }
      

      
      if (_currentIndex == self.lives.count - 1)
      {
        _downLive = [self.lives firstObject];
      } else if (_currentIndex == self.lives.count)
      {
        _downLive = self.lives[1];
        _currentIndex = 0;
        
      } else
      {
        _downLive = self.lives[_currentIndex+1];
      }
      [self prepareForImageView:self.downImageView withLive:_downLive];
      
      
      
      if (self.downPlayer.view.frame.origin.y == 0) {
        self.downPlayer.view.frame = CGRectMake(0, SCREEN_HEIGHT * 2, SCREEN_WIDTH, SCREEN_HEIGHT);
      }else{
        self.downPlayer.view.frame = CGRectMake(0, self.downPlayer.view.frame.origin.y - SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
      }
      
      if (self.upPerPlayer.view.frame.origin.y == SCREEN_HEIGHT * 2) {
        [self prepareForVideo:self.upPerPlayer withLive:_downLive];
      }
      if (self.middlePlayer.view.frame.origin.y == SCREEN_HEIGHT * 2) {
        [self prepareForVideo:self.middlePlayer withLive:_downLive];
      }
      if (self.downPlayer.view.frame.origin.y == SCREEN_HEIGHT * 2) {
        [self prepareForVideo:self.downPlayer withLive:_downLive];
      }
      
      
      
      if (_previousIndex == _currentIndex) {
        return;
      }
      if ([self.playerDelegate respondsToSelector:@selector(playerScrollView:currentPlayerIndex:)]) {
        [self.playerDelegate playerScrollView:self currentPlayerIndex:_currentIndex];
        _previousIndex = _currentIndex;
        NSLog(@"current index in delegate: %ld -%s",_currentIndex,__FUNCTION__);
      }
    }
    else if (offset <= 0)
    {
      // slides to the upper player
      scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
      _currentIndex--;
      self.downImageView.image = self.middleImageView.image;

      if (self.downPlayer.view.frame.origin.y == 2 * SCREEN_HEIGHT) {
        self.downPlayer.view.frame = CGRectMake(0, SCREEN_HEIGHT * 0, SCREEN_WIDTH, SCREEN_HEIGHT);
      }else{
        self.downPlayer.view.frame = CGRectMake(0, self.downPlayer.view.frame.origin.y + SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
      }
      
      self.middleImageView.image = self.upperImageView.image;

      
      if (self.middlePlayer.view.frame.origin.y == 2 * SCREEN_HEIGHT) {
        self.middlePlayer.view.frame = CGRectMake(0, SCREEN_HEIGHT * 0, SCREEN_WIDTH, SCREEN_HEIGHT);
      }else{
        self.middlePlayer.view.frame = CGRectMake(0, self.middlePlayer.view.frame.origin.y + SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
      }
     
      
      
      if (_currentIndex == 0)
      {
        _upperLive = [self.lives lastObject];
        
      } else if (_currentIndex == -1)
      {
        _upperLive = self.lives[self.lives.count - 2];
        _currentIndex = self.lives.count-1;
        
      } else
      {
        _upperLive = self.lives[_currentIndex - 1];
      }
      [self prepareForImageView:self.upperImageView withLive:_upperLive];
      
      if (self.upPerPlayer.view.frame.origin.y == 2 * SCREEN_HEIGHT) {
        self.upPerPlayer.view.frame = CGRectMake(0, SCREEN_HEIGHT * 0, SCREEN_WIDTH, SCREEN_HEIGHT);
      }else{
        self.upPerPlayer.view.frame = CGRectMake(0, self.upPerPlayer.view.frame.origin.y + SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
      }
      
      if (self.upPerPlayer.view.frame.origin.y == 0 ) {
        [self prepareForVideo:self.upPerPlayer withLive:_upperLive];
      }
      if (self.middlePlayer.view.frame.origin.y == 0 ) {
        [self prepareForVideo:self.middlePlayer withLive:_upperLive];
      }
      if (self.downPlayer.view.frame.origin.y == 0 ) {
        [self prepareForVideo:self.downPlayer withLive:_upperLive];
      }
      
      if (_previousIndex == _currentIndex) {
        return;
      }
      if ([self.playerDelegate respondsToSelector:@selector(playerScrollView:currentPlayerIndex:)]) {
        [self.playerDelegate playerScrollView:self currentPlayerIndex:_currentIndex];
        _previousIndex = _currentIndex;
        NSLog(@"current index in delegate: %ld -%s",_currentIndex,__FUNCTION__);
      }
    }
  }
}
  [[NSNotificationCenter defaultCenter]addObserver:self
                                          selector:@selector(handlePlayerNotify:)
                                              name:(MPMoviePlayerFirstVideoFrameRenderedNotification)
                                            object:nil];
  [[NSNotificationCenter defaultCenter]addObserver:self
                                          selector:@selector(handlePlayerPreparedToPlayNotify:)
                                              name:(MPMediaPlaybackIsPreparedToPlayDidChangeNotification)
                                            object:nil];

Demo传送门

上一篇下一篇

猜你喜欢

热点阅读