視頻播放器的封裝(支持橫豎屏切換、cell上播放)

用了幾天斷斷續(xù)續(xù)的空閑時間封裝了一個視頻播放器ZWPlayer,支持橫豎屏切換、cell上播放、滾動UITableView停止視頻、以及滾動UITableViewcell視頻播放器縮小到底部播放,具體請看如下效果圖。這篇文章主要是講這個視頻播放器封裝的總體思路。針對具體細節(jié)并沒有說太多,這里主要是針對幾個自己感覺值得注意的地方說明了下。具體實現(xiàn)請看Demo,Demo下載地址:https://github.com/ZhengYaWei1992/ZWPlayer

cell上播放動畫效果
全屏豎屏狀態(tài)
全屏橫屏狀態(tài)
非全屏狀態(tài)

在研究視頻播放相關(guān)的問題之前必須能弄懂一個很重要的問題,如何正確的控制橫豎屏切換問題。具體請參考我之前寫的文章:http://www.lxweimin.com/p/5be7eaf4a1a6。這里簡簡單說下,一般在項目中控制橫豎屏問題,主要代碼一般會寫在UITabbarController中。而不是直接寫在UIViewController中,因為即使寫在UIViewController在push的情況下也不會起到什么作用,最終的控制權(quán)是有UITabbarController控制的。當然這種情況只是針對push而言,如果僅僅是針對模態(tài)進入的控制器,情況又會完全相反。看看tabBarController中是如何控制屏幕是否支持旋轉(zhuǎn)的。一般主要是重寫系統(tǒng)的下面另個方法,來控制特定頁面是否支持旋轉(zhuǎn)。這里只是簡單說說,想了解更多,就參考上面的連接。

- (BOOL)shouldAutorotate{
    UINavigationController *nav = self.viewControllers[self.selectedIndex];
    if ([nav.topViewController isKindOfClass:[ViewController2 class]]) {
       return YES;
    }else if ([nav.topViewController isKindOfClass:[A_A_ViewController class]]){
        return YES;
    }
    return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    UINavigationController *nav = self.selectedViewController;
    if ([nav.topViewController isKindOfClass:[ViewController2 class]]) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }else if ([nav.topViewController isKindOfClass:[A_A_ViewController class]]){
         return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    // 其他頁面
    return UIInterfaceOrientationMaskPortrait;
}

說下視頻播放器的層次結(jié)構(gòu),這里我將控制層和播放曾完全給剝離開來編寫代碼,在視頻播放器的最上層是一個透明的UIView,這個UIView上放置了多個控件,諸如:返回按鈕、暫停、加載提示、時間label、UISlider、精度條以及全屏按鈕。而播放層主要用于播放視屏,處理一些業(yè)務(wù)邏輯,諸如屏幕旋轉(zhuǎn)、緩沖進度計算、視屏播放狀態(tài)監(jiān)控以及是否處于前臺、是否退出應(yīng)用等。所以在實際風裝這個視頻播放器的過程中,主要是創(chuàng)建了兩個類ZWPlayerControlView(控制層)和ZWPlayerView(處理播放相關(guān)業(yè)務(wù)邏輯),以及一個ZWPlayer,這個類主要是對外提供接口,以及一些常量的宏都放置在這個類里面。

控制層布局雖然簡單,但是有一點值得去說的。可能會有人疑惑類似這種帶有加載精度的控制條是怎么弄得,可能有人會猜測UISlider難道還有一些其他自己不知道的屬性。其實并不是這樣的,帶有緩沖的進度條主要是通過UISlider和UIProgerssView兩個控件相互結(jié)合的。其中UIProgressView主要負責顯示加載進度,剩下的播放進度、寬進、后退都是由UISlider完成的。

看一下ZWPlayer中是如何監(jiān)聽屏幕旋轉(zhuǎn)的。這里監(jiān)聽屏幕旋轉(zhuǎn)的主要目的是為了更變?nèi)涟粹o的狀態(tài),至于控制層使用的是Masonry設(shè)置的約束。

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onDeviceOrientationChange)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil
     ];


- (void)onDeviceOrientationChange{
    if (_isCellVideo) {
        return;
    }
    UIDeviceOrientation orientation             = [UIDevice currentDevice].orientation;
    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;
    switch (interfaceOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:{
            self.controlView.fullScreenBtn.selected = YES;
            self.isFullScreen = YES;
        }
            break;
        case UIInterfaceOrientationPortrait:{
            self.isFullScreen = !self.isFullScreen;
            self.controlView.fullScreenBtn.selected = NO;
        
            self.isFullScreen = NO;
            
        }
            break;
        case UIInterfaceOrientationLandscapeLeft:{
            self.controlView.fullScreenBtn.selected = YES;
     
            self.isFullScreen = YES;
        }
            break;
        case UIInterfaceOrientationLandscapeRight:{
            self.controlView.fullScreenBtn.selected = YES;
            self.isFullScreen = YES;
        }
            break;
            
        default:
            break;
    }
}

雖然上面的代碼片段可以在設(shè)備屏幕旋轉(zhuǎn)的時候,更新全屏按鈕的狀態(tài)。但針對于設(shè)備并沒有旋轉(zhuǎn),點擊了全屏按鈕在豎屏和橫屏之間切換,這又是如何做到的呢?這里主要是強制更新設(shè)備方向,具體請看如下代碼片段。下面的代碼片段針對ARC和非ARC兩種情況均提供了解決方案,但是有一點要注意: [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];這具代碼不能直接拿來使用,否則可能會被拒絕上架。

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{
    // arc下
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector             = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val                  = orientation;
        // 從2開始是因為0 1 兩個參數(shù)已經(jīng)被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    /*
     // 非arc下
     if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
     [[UIDevice currentDevice] performSelector:@selector(setOrientation:)
     withObject:@(orientation)];
     }
     // 直接調(diào)用這個方法通不過apple上架審核
     [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
     */
}

針對于常規(guī)的視屏播放就講這么多,下面針對cell上播放視頻簡單的說一下。cell上播放視屏,首先要考慮一點,ZWPlayer負責播放視頻的這個類,最好是創(chuàng)建一個單例對象。除了這一點,接下來著重要考慮的兩點是關(guān)于復用的問題以及如何監(jiān)控UITableView的滾啊東,判斷當前播放器是否依然在可視區(qū)域。

首先來看看如何在滾動UITablview的時候解決復用問題。如下代碼主要是寫在創(chuàng)建UITablViewCell的一個代理方法中。

if (indexPath.row== _currentIndexPath.row) {
                [cell.playBtn.superview sendSubviewToBack:cell.playBtn];
            }else{
                [cell.playBtn.superview bringSubviewToFront:cell.playBtn];
            }
            NSArray *indexpaths = [tableView indexPathsForVisibleRows];
            if (![indexpaths containsObject:_currentIndexPath]) {//復用
                if ([[UIApplication sharedApplication].keyWindow.subviews containsObject:_playerView]) {
                    _playerView.hidden = NO;
                }else{
                    _playerView.hidden = YES;
                }
            }else{
                if ([cell.picView.subviews containsObject:_playerView]) {
                    [cell.picView addSubview:_playerView];
                    [_playerView play];
                    _playerView.hidden = NO;
                }
            }

再來看看,如何判斷當前播放視屏的cell是否已經(jīng)滾出可視區(qū)范圍之內(nèi)。

//tableViewCell離開界面,視頻消失
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        if(_playerView == nil){
            return;
        }
        if (_playerView.superview) {//播放的cell在當前屏幕可視區(qū)
            CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:_currentIndexPath];
            CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
            
            if (rectInSuperview.origin.y<-self.currentCell.picView.frame.size.height||rectInSuperview.origin.y>[UIScreen mainScreen].bounds.size.height-64-49) {//往上拖動
                if ([[UIApplication sharedApplication].keyWindow.subviews containsObject:_playerView]&&_isBottomVideo) {
                    _isBottomVideo = YES;
                }else{
                    //播放視頻的cell不在當前可視區(qū)范圍內(nèi),放到window,在底部顯示
                    [self toBottomVideo];
                }
            }else{
                if ([self.currentCell.picView.subviews containsObject:_playerView]) {
                    
                }else{
                    //播放視頻的cell在當前可視區(qū)范圍內(nèi),回到原來的cell上繼續(xù)播放
                    [self toCell];
                }
            }
        }
    }
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容