重構點

播放器部分

播放器部分模塊化,播放器作為一個獨立的模塊,

現階段代碼

- (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];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 37.cocoa內存管理規則 1)當你使用new,alloc或copy方法創建一個對象時,該對象的保留計數器值為1...
    如風家的秘密閱讀 893評論 0 4
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,252評論 4 61
  • 有一個花匠,家里世世代代都是種花巧匠,在他很小的時候父親就教他種花的方法,得天獨厚的條件讓他在種花方面有超強的優勢...
    瑤YY閱讀 321評論 0 2
  • 今天是七發成勢公開課,今天感覺自己收獲特別大! 從早上自己的主持,開始,我發現自己還是原來那個充滿能量的妹紙!一件...
    竹清運營八陣圖閱讀 268評論 0 0