播放器部分
播放器部分模塊化,播放器作為一個獨立的模塊,
現階段代碼
- (void)buildPlayerByPlayType:(DQPlayerType)type isFirstBuild:(BOOL)isFirst {
if (self.playerView) {
[self.playerView stop];
self.playerView = nil;
}
switch (type) {
case DQPlayerType_default: {
self.playerView = self.Leplayer;
}
break;
case DQPlayerType_pptv: {
self.playerView = self.pptvPlayer;
}
break;
case DQPlayerType_tencent: {
self.playerView = self.tencentPlayer;
}
break;
default:
break;
}
if (!isFirst) {
[self _firstTimeSetup];
}
[self.contentView addSubview:self.playerView];
}
- (void)_firstTimeSetup {
//這步是為了解決什么bug么?
if (self->_bUserWantBack) {
return;
}
//屬于播放器初始化的屬性
self.playerView.bIsLocalFile = self.bIsLocalFile;
//
[UIApplication sharedApplication].idleTimerDisabled = YES;
NSError *error = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback
error:&error];
if (!success) {
// Handle error here, as appropriate
//這里要加LogError
}
//這個函數才是初始化播放器的
[self _setupPlayerRelated];
//這個函數屬于上報相關,不屬于播放器
[self resetReportItemsNeedAddUUID:YES];
//這個函數包含了上報的參數整理、上報、UI和按鈕狀態、播放等
[self _setupCurrentVideoAndPlay];
//上報用的參數
self->_startTime = [NSDate date];
//讀取播放歷史
if (self.lastPlayTime <= 0.0f) {
[self initLastPlayTimeWithHistoryRecords];
}
//根據不同的播放狀態來處理播放歷史
if (self.bVideoPlayable) {
[self recordHistoryForIndex:self.curPlayIndex passedDuration:self.playerView.timeplayed totalDuration:self.playerView.duration];
} else {
[self recordHistoryForIndex:self.curPlayIndex passedDuration:self.lastPlayTime totalDuration:self.playerView.duration];
}
//監聽App生命周期的一些事件,這部分也可以歸于播放器模塊,用接口或者block把時機暴漏給外層。
[self _setupGlobalObservers];
}
@protocol DQOnlinePlayDataProtocol
- (NSString *)url;
- (NSString *)title;
@end
@protocol DQPlayerProtocol
//獲取
- (UIViewController*)playerViewController;
- (void)refreshPlayerWithData:(id<DQOnlinePlayDataProtocol>)data;
- (void)refreshPlayerWithLiveData:(id<DQLivePlayDataProtocol>)data;
- (void)refreshPlayerWithLocalData:(id<DQLocalPlayDataProtocol>)data;
//狀態獲取
- (BOOL)isScreenLocked;
- (NSTimeInterval)playerInterval;
- (NSTimeInterval)duration;
//監聽事件,這也可以獨立出另外一個協議
- (void)playerStatusDidChanged:(DQPlayerStat)status;
.....
@end
方法命名
[self _setupCurrentVideoAndPlay];
- 拆分函數,函數功能和函數名統一意義
- 函數方法命名形式,-規范
上報時機化
上報的時機化(需要討論),需要業務層支持。這里在樂搜之前執行過,由于上報點和業務點往往不是同一個點,有時需要增加很多方法跨多層傳遞;這里提出用通知來發消息上報,上報點只發時機,接受的上報business負責整理參數并發送給上報引擎。
- (void)LesoReportEvent_Homepage_PageView:(LesoHomePageDataModel*)model {
LesoPadReportParams*params = [[LesoPadReportParams alloc]init];
params.module =@"home_page";
[[LesoPadReportEngine sharedLesoEngine]reportHomepage:params];
}
//首頁pv上報
LesoSearchResultReportModel * model = [[LesoSearchResultReportModel alloc] init];
model.eventType = LesoReportEvent_Homepage_PageView;
[[NSNotificationCenter defaultCenter] postNotificationName:LESOREPORT_NOTIFICATION object:model];
[_errorView removeFromSuperview];
同時只存在一個播放器,當需要播放其他視頻時刷新播放器和詳情頁即可。
[self pushNewDetail:releData];
變成
[self refresWithData:releData];