ZFPlayer 全屏、横竖屏使用小记

关于这个库大家都不陌生,下面小结下自己使用过程中的经验,主要是关于全屏横竖屏的几个小点。

使用cell上直接播放的创建方式(先小屏播放,然后点击全屏按钮),全屏后完全取决于外部设置的全屏模式(强制改变后会有问题)

_player = [ZFPlayerController playerWithScrollView:self.tableView playerManager:playerManager containerViewTag:100];

使用普通模式实现下面的分享有效果

 _player = [[ZFPlayerController alloc] initWithPlayerManager:playerManager containerView:[UIApplication sharedApplication].keyWindow];

 

全屏的两种方式

1、ZFPlayerController

 [self.player enterFullScreen:YES animated:NO];

全屏  ZFPlayerControlView设置的全屏模式必须建立在player存在的情况下

2、ZFPlayerControlView

 fullScreenOnly

 全屏,  竖屏有全屏按钮 无返回按钮 必须到横屏才有返回按钮 设置的模式不再受影响

 

横竖屏的两个影响属性(ZFPlayerController)

1、 lockedScreen 默认NO

锁定屏幕, 什么操作都添加不了, 设置为YES时不论系统是否锁定竖屏 ,都可以按照预定的横竖屏显示

2、allowOrentitaionRotation 默认YES

是否允许播放器旋转 ,可以正常自定义添加标题、时间、返回按钮等等 ,设置为NO时不论系统是否锁定竖屏, 都可以按照预定的横竖屏显示

设置为YES时,会根据手机旋转方向自动调整,但初始化时会按照自定义的方向初始化

 

- (ZFPlayerController *)player {
    if (!_player) {
        
        ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];

        _player = [[ZFPlayerController alloc] initWithPlayerManager:playerManager containerView:[UIApplication sharedApplication].keyWindow];
        _player.controlView = self.controlView;
        _player.disableGestureTypes = ZFPlayerDisableGestureTypesDoubleTap | ZFPlayerDisableGestureTypesPan | ZFPlayerDisableGestureTypesPinch;
        _player.WWANAutoPlay = YES;
//        锁定屏幕 什么操作都添加不了
//        _player.lockedScreen = YES;
//        不允许屏幕旋转
        _player.allowOrentitaionRotation = NO;
        @weakify(self)
        _player.playerLoadStateChanged = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset, ZFPlayerLoadState loadState) {
            if (loadState == ZFPlayerLoadStatePlaythroughOK) {
                [UIView animateWithDuration:0.5 animations:^{
                    @strongify(self)
                    self.controlView.backgroundColor = [UIColor clearColor];
                }];
            }
        };
    }
    return _player;
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [[ZFPlayerControlView alloc] init];
//      全屏  竖屏有全屏按钮 无返回按钮 必须到横屏才有返回按钮
//        _controlView.fullScreenOnly = YES;
    }
    return _controlView;
}

#pragma mark - Event
- (void)playVideo:(NSString *) videoUrl
 videoSnapshotUrl:(NSString *) videoSnapshotUrl {
    if (!videoUrl.length) {
        return;
    }
    
    if (self.controlView) {
           self.controlView = nil;
       }
    self.controlView.backgroundColor = [UIColor blackColor];
    
    
    if (self.player) {
        self.player = nil;
    }
    
    @weakify(self)
    self.player.gestureControl.singleTapped = ^(ZFPlayerGestureControl * _Nonnull control) {
        @strongify(self)
        [UIView animateWithDuration:0.2 animations:^{
            self.player.containerView.alpha = 0.5;
        }completion:^(BOOL finished) {
            [self.player stop];
            self.player.containerView.alpha = 1;
            self.player = nil;

        }];

    };

    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        @strongify(self)
        [self.player.currentPlayerManager replay];
    };
// [self.player enterFullScreen:YES animated:NO]; 情况下必须放到这里 不然会失效  _controlView.fullScreenOnly = YES; 情况下不需要这样 但是竖屏无法返回 必须到横屏才有返回按钮
    [self.controlView showTitle:@""
    coverURLString:videoSnapshotUrl
    fullScreenMode: ZFFullScreenModePortrait];
    [self.player enterFullScreen:YES animated:NO];
    self.player.currentPlayerManager.assetURL = [NSURL URLWithString:videoUrl];
   
   
}

- (void)playVideo:(NSString *) videoUrl
 videoSnapshotUrl:(NSString *) videoSnapshotUrl
        videoName:(NSString *) videoName
      imageFormat:(iComeImageFormat) imageFormat{
    if (!videoUrl.length) {
         return;
     }
     
     if (self.controlView) {
         self.controlView = nil;
     }
     self.controlView.backgroundColor = [UIColor blackColor];
    
     @weakify(self)
     self.controlView.backBtnClickCallback = ^{
        @strongify(self)
         [UIView animateWithDuration:0.2 animations:^{
             self.player.containerView.alpha = 0.5;
         }completion:^(BOOL finished) {

             [self.player stop];
             self.player.containerView.alpha = 1;
             self.player = nil;

         }];
    };
    
     if (self.player) {
         self.player = nil;
     }
    
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        @strongify(self)
        [self.player.currentPlayerManager seekToTime:0 completionHandler:^(BOOL finished) {
            [self.player.currentPlayerManager pause];
        }];
    };
    
    [self.controlView showTitle:videoName
    coverURLString:videoSnapshotUrl
    fullScreenMode: (imageFormat == iComeImageFormatVertical ? ZFFullScreenModePortrait : ZFFullScreenModeLandscape)];
    [self.player enterFullScreen:YES animated:NO];
    self.player.currentPlayerManager.assetURL = [NSURL URLWithString:videoUrl];
}

- (void)playVideo:(NSString *) videoPath
       coverImage:(UIImage *) coverImage {
    
    if (!videoPath.length) {
        return;
    }
    
    if (self.controlView) {
        self.controlView = nil;
    }
    
    if (self.player) {
       self.player = nil;
    }
    
    @weakify(self)
    self.player.gestureControl.singleTapped = ^(ZFPlayerGestureControl * _Nonnull control) {
        @strongify(self)
        [UIView animateWithDuration:0.2 animations:^{
            self.player.containerView.alpha = 0.5;
        }completion:^(BOOL finished) {
            [self.player stop];
            self.player.containerView.alpha = 1;
            self.player = nil;

        }];

    };

    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        @strongify(self)

        [self.player.currentPlayerManager replay];
    };
    
    [self.controlView showTitle:@""
        coverImage:coverImage
    fullScreenMode:ZFFullScreenModePortrait];
    [self.player enterFullScreen:YES animated:NO];
    self.player.currentPlayerManager.assetURL = [NSURL fileURLWithPath:videoPath];
}

 

posted @ 2019-09-26 17:44  小菜看代码  阅读(7067)  评论(0编辑  收藏  举报