重构点

2017-05-25  本文已影响6人  可能是老曹

播放器部分

播放器部分模块化,播放器作为一个独立的模块,

现阶段代码

- (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];
上一篇 下一篇

猜你喜欢

热点阅读