4_1-带货场景(页面)
1 功能概述
帶貨場景的頁面實現為 PLVECWatchRoomViewController
帶貨場景下定義的 直播模式、回放模式 的 共用界面。 直播支援的功能有:播放器、聊天室、商品、打賞、互動應用。 回放支援的功能有:播放器。
2 使用介紹
由於頻道資訊、登入使用者帳號資訊等帶貨頁面所需的初始化參數,在登入時已經透過類別 PLVRoomLoginClient 進行了配置,並在 PLVRoomLoginClient 內部使用 PLVRoomDataManager 類別的單例對這些直播間數據資訊進行了管理,進入帶貨場景的頁面只需要以下程式碼:
PLVECWatchRoomViewController *vctrl = [[PLVECWatchRoomViewController alloc] init];
[self.navigationController pushViewController:vctrl animated:YES];
離開帶貨頁面除了把頁面從頁面堆疊移出,務必呼叫 PLVRoomLoginClient 的登出介面,移除 PLVRoomDataManager 單例對當前直播間數據的持有和監聽:
[PLVRoomLoginClient logout];
[self.navigationController popViewControllerAnimated:YES];
3 頁面介紹
3.1 頁面 UI
帶貨場景頁面,不管是直播還是回放,都由以下頁面 UI 控制項組成:
@property (nonatomic, strong) PLVECPlayerViewController *playerVC; // 页面底部播放控制器
@property (nonatomic, strong) UIScrollView *scrollView; // 播放器顶部横行透明滚动视图
@property (nonatomic, strong) PLVECLiveDetailPageView *liveDetailPageView; // 滚动视图第一屏
@property (nonatomic, strong) PLVECHomePageView *homePageView; // 滚动视图第二屏
@property (nonatomic, strong) UIButton *closeButton; // 右上角关闭按钮
其中,scrollView 有三屏,第一屏 liveDetailPageView 是顯示公告資訊、直播介紹,第二屏 homePageView 如果是直播,顯示聊天室、按讚按鈕、商品按鈕、購物車按鈕、禮物按鈕等互動控制項,如果是回放,顯示回放影片進度條和切換影片播放速率等按鈕等互動控制項,第三屏為空白,用於使用者希望無干擾觀看時使用。進入頁面預設顯示第二屏。播放器位於 scrollView 底部,關閉按鈕位於scrollView 上方。
檢視結構如下:
.
└── PLVECWatchRoomViewController
├── PLVECPlayerViewController
├── UIScrollView
│ ├── PLVECLiveDetailPageView
│ └── PLVECHomePageView
└── UIButton
其次,帶貨場景頁面不支援橫屏,只支援豎屏,因此需對父類別 UIViewController 的以下方法進行覆寫:
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
3.2 直播間數據監聽
頁面需要對直播間數據 PLVRoomData 進行即時監聽並作出相應的 UI 更新,首先,需要導入標頭檔 #import "PLVRoomDataManager.h",並讓 PLVECWatchRoomViewController 遵循協定 PLVRoomDataManagerProtocol,然後添加監聽,程式碼示例如下:
[[PLVRoomDataManager sharedManager] addDelegate:self delegateQueue:dispatch_get_main_queue()];
程式碼示例中,delegateQueue 參數傳入 dispatch_get_main_queue() 表示希望回呼方法從主執行緒執行,這是由於當前頁面相關回呼裡執行的操作都是更新 UI。
關於 PLVRoomDataManager 的更多介紹,詳見文件《6_2 核心common-數據》。
帶貨場景頁面需要監聽的 delegate 方法如下:
#pragma mark PLVRoomDataManager Protocol
/// 在线人数 onlineCount 更新
- (void)roomDataManager_didOnlineCountChanged:(NSUInteger)onlineCount {
}
/// 点赞数 likeCount 更新
- (void)roomDataManager_didLikeCountChanged:(NSUInteger)likeCount {
}
/// 播放状态 playing 更新
- (void)roomDataManager_didPlayingStatusChanged:(BOOL)playing {
}
/// 菜单信息 menuInfo 更新
- (void)roomDataManager_didMenuInfoChanged:(PLVLiveVideoChannelMenuInfo *)menuInfo {
}
/// 直播状态 liveState 更新
- (void)roomDataManager_didLiveStateChanged:(PLVChannelLiveStreamState)liveState {
}
監聽的移除在 2 使用介紹 裡面提到的 PLVRoomLoginClient 的登出方法 +logout 內部進行,所以無需額外做移除監聽的操作。
