播放器部分
播放器部分模塊化,播放器作為一個(gè)獨(dú)立的模塊,
現(xiàn)階段代碼
- (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
}
//這個(gè)函數(shù)才是初始化播放器的
[self _setupPlayerRelated];
//這個(gè)函數(shù)屬于上報(bào)相關(guān),不屬于播放器
[self resetReportItemsNeedAddUUID:YES];
//這個(gè)函數(shù)包含了上報(bào)的參數(shù)整理、上報(bào)、UI和按鈕狀態(tài)、播放等
[self _setupCurrentVideoAndPlay];
//上報(bào)用的參數(shù)
self->_startTime = [NSDate date];
//讀取播放歷史
if (self.lastPlayTime <= 0.0f) {
[self initLastPlayTimeWithHistoryRecords];
}
//根據(jù)不同的播放狀態(tài)來(lái)處理播放歷史
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];
}
//監(jiān)聽(tīng)App生命周期的一些事件,這部分也可以歸于播放器模塊,用接口或者block把時(shí)機(jī)暴漏給外層。
[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;
//狀態(tài)獲取
- (BOOL)isScreenLocked;
- (NSTimeInterval)playerInterval;
- (NSTimeInterval)duration;
//監(jiān)聽(tīng)事件,這也可以獨(dú)立出另外一個(gè)協(xié)議
- (void)playerStatusDidChanged:(DQPlayerStat)status;
.....
@end
方法命名
[self _setupCurrentVideoAndPlay];
- 拆分函數(shù),函數(shù)功能和函數(shù)名統(tǒng)一意義
- 函數(shù)方法命名形式,-規(guī)范
上報(bào)時(shí)機(jī)化
上報(bào)的時(shí)機(jī)化(需要討論),需要業(yè)務(wù)層支持。這里在樂(lè)搜之前執(zhí)行過(guò),由于上報(bào)點(diǎn)和業(yè)務(wù)點(diǎn)往往不是同一個(gè)點(diǎn),有時(shí)需要增加很多方法跨多層傳遞;這里提出用通知來(lái)發(fā)消息上報(bào),上報(bào)點(diǎn)只發(fā)時(shí)機(jī),接受的上報(bào)business負(fù)責(zé)整理參數(shù)并發(fā)送給上報(bào)引擎。
- (void)LesoReportEvent_Homepage_PageView:(LesoHomePageDataModel*)model {
LesoPadReportParams*params = [[LesoPadReportParams alloc]init];
params.module =@"home_page";
[[LesoPadReportEngine sharedLesoEngine]reportHomepage:params];
}
//首頁(yè)pv上報(bào)
LesoSearchResultReportModel * model = [[LesoSearchResultReportModel alloc] init];
model.eventType = LesoReportEvent_Homepage_PageView;
[[NSNotificationCenter defaultCenter] postNotificationName:LESOREPORT_NOTIFICATION object:model];
[_errorView removeFromSuperview];
同時(shí)只存在一個(gè)播放器,當(dāng)需要播放其他視頻時(shí)刷新播放器和詳情頁(yè)即可。
[self pushNewDetail:releData];
變成
[self refresWithData:releData];