搭建直播平台时,需要将房间列表设置为“上下滑动”的样式,这种样式要如何通过代码实现呢?本文《搭建直播平台,在线直播源码实现列表上下滑动的方式》将详细解读这一点:
1、 首先要在观看的控制器中声明两个属性
@property(nonatomic,strong)NSArray *listArray; @property(nonatomic,assign)NSInteger currentIndex;2、在观看控制器.m文件中创建一个滚动视图(changeRoomScroll),并设置分页,同时在滚动视图上加入主播的头像模糊图;
- (UIScrollView *)changeRoomScroll { if (!_changeRoomScroll) { _changeRoomScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _window_width, _window_height)]; _changeRoomScroll.delegate = self; _changeRoomScroll.pagingEnabled = YES; _changeRoomScroll.showsVerticalScrollIndicator = NO; _changeRoomScroll.bounces = NO; _changeRoomScroll.contentSize = CGSizeMake(_window_width, _window_height*_listArray.count); _changeRoomScroll.contentOffset = CGPointMake(0, _window_height*_currentIndex); for (int i=0; i<_listArray.count; i++) { NSDictionary *listDic = _listArray[i]; UIImageView *avaEffect = [PublicObj getAvatarEffectWithUrl:minstr([listDic valueForKey:@"avatar"])]; avaEffect.frame = CGRectMake(0, _window_height*i, _window_width, _window_height); [_changeRoomScroll addSubview:avaEffect]; } } return _changeRoomScroll; }3、 在线直播源码实现滚动视图的两个代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == _changeRoomScroll) { _playCtrlView.keyBoardIsShow = NO; [_playCtrlView.chatTool.chatTF resignFirstResponder]; _changeScorllOffSetY = _changeRoomScroll.contentOffset.y; } } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == _changeRoomScroll) { _changeScorllOffSetY = _changeRoomScroll.contentOffset.y; if (_changeScorllOffSetY < 0 || (_currentIndex == _changeScorllOffSetY/_window_height)) { return; } //[MBProgressHUD showMessage:@""]; YBWeakSelf; [YBNetworking postWithUrl:@"Live.checkLive" Dic:@{@"liveuid":minstr([newPlayDic valueForKey:@"uid"]),@"stream":minstr([newPlayDic valueForKey:@"stream"])} Suc:^(int code, id info, NSString *msg) { [MBProgressHUD hideHUD]; if (code == 0) { [weakSelf changeRoomRest]; }else { [MBProgressHUD showPop:msg]; } } Fail:^(id fail) { [MBProgressHUD hideHUD]; }]; } } 4.滚动完成将播放器归位,完成在线直播源码房间切换 -(void)changeRoomRest { [self releaseAllOfChangeRoom:YES]; _userType = @"30"; _firstStar = 0; [_playCtrlView clearChatList]; _playCtrlView.playDic = _playDic; _changeRoomScroll.contentOffset = CGPointMake(0, _window_height*_currentIndex); _playCtrlView.frame = CGRectMake(0, _window_height*_currentIndex, _window_width, _window_height); _videoPlayView.frame = CGRectMake(0, _window_height*_currentIndex, _window_width, _window_height); [self bringPlayerToFront:NO]; if(_txLivePlayer != nil) { if ([minstr([_playDic valueForKey:@"anyway"]) isEqual:@"1"]) { [_txLivePlayer setRenderMode:RENDER_MODE_FILL_EDGE]; } if ([minstr([_playDic valueForKey:@"isvideo"]) isEqual:@"1"]) { [_txLivePlayer setRenderMode:RENDER_MODE_FILL_EDGE]; } NSString *playUrl = [_playDic valueForKey:@"pull"]; NSInteger _playType = 0; if ([playUrl hasPrefix:@"rtmp:"]) { _playType = PLAY_TYPE_LIVE_RTMP; } else if (([playUrl hasPrefix:@"https:"] || [playUrl hasPrefix:@"http:"]) && [playUrl rangeOfString:@".flv"].length > 0) { _playType = PLAY_TYPE_LIVE_FLV; }else{ _playType = PLAY_TYPE_VOD_MP4; } if ([playUrl rangeOfString:@".mp4"].length > 0) { _playType = PLAY_TYPE_VOD_MP4; } if ([playUrl rangeOfString:@".m3u8"].length > 0) { _playType = PLAY_TYPE_VOD_FLV; } int result = [_txLivePlayer startPlay:playUrl type:_playType]; if( result == 0){ NSLog(@"播放视频"); }else { [MBProgressHUD showPop:YZMsg(@"播放失败")]; [self dismissVC]; } } [self enterRoom]; [self showTitle]; }以上就是《搭建直播平台,在线直播源码实现列表上下滑动的方式》的全部内容,搭建直播平台不是一件容易的事,在线直播源码中处处是坑,在播放器方面,云豹也是踩过很多坑,以后会陆续将直播源码开发经验编纂成博客发布出来的。
声明:本文由csdn作者:云豹网络科技原创,转载请注明作者名称,否则拒绝转载