Skip to content

App内小浮窗模式

changsanjiang edited this page Jan 14, 2021 · 4 revisions

模式1: 当viewController退出时切换为小浮窗播放

1610596068847406.mp4

配置代码如下:

// step 0: 导入头文件
#import <SJVideoPlayer/SJVideoPlayer.h>
#import "SJFloatSmallViewTransitionController.h"

@interface SJFloatModeDemoViewController1 ()
@property (nonatomic, strong) SJVideoPlayer *player;
@property (nonatomic) NSInteger videoId;
@end

@implementation SJFloatModeDemoViewController1
// step 1: 为ViewController自定义初始化方法(用于比较需要返回的控制器在当前window中的是否存在) 
+ (instancetype)viewControllerWithVideoId:(NSInteger)videoId {
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    SJFloatModeDemoViewController1 *instance = nil;
    // 比较需要返回的控制器在当前window中的是否存在
    // 如果存在则返回该控制器, 否则创建一个新的控制器
    for ( __kindof UIViewController *vc in window.SVTC_playbackInFloatingViewControllers ) {
        if ( [vc isKindOfClass:SJFloatModeDemoViewController1.class] ) {
            SJFloatModeDemoViewController1 *playbackViewController = vc;
            if ( playbackViewController.videoId == videoId ) {
                instance = playbackViewController;
                break;
            }
        }
    }
    
    if ( instance == nil ) {
        instance = [SJFloatModeDemoViewController1.alloc initWithVideoId:videoId];
    }
    return instance;
}

- (instancetype)initWithVideoId:(NSInteger)videoId {
    self = [super init];
    if ( self ) {
        _videoId = videoId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    
    _player = SJVideoPlayer.player;
    _player.resumePlaybackWhenAppDidEnterForeground = YES;
    _player.defaultEdgeControlLayer.fixesBackItem = YES;
    _player.URLAsset = [SJVideoPlayerURLAsset.alloc initWithURL:SourceURL0];
    [self.view addSubview:_player.view];
    [_player.view mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        } else {
            make.top.offset(20);
        }
        make.left.right.offset(0);
        make.height.equalTo(self.view.mas_width).multipliedBy(9/16.0);
    }];
     
    // step 2: 设置小浮窗控制器
    _player.floatSmallViewController = SJFloatSmallViewTransitionController.alloc.init;
    __weak typeof(self) _self = self;
    _player.floatSmallViewController.doubleTappedOnTheFloatViewExeBlock = ^(id<SJFloatSmallViewController>  _Nonnull controller) {
        __strong typeof(_self) self = _self;
        if ( self == nil ) return;
        self.player.isPaused ? [self.player play] : [self.player pause];
    }; 
}

// step 3: 返回当前小浮窗控制器
- (SJFloatSmallViewTransitionController *_Nullable)SVTC_floatSmallViewTransitionController {
    return (SJFloatSmallViewTransitionController *)_player.floatSmallViewController;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // step 4
    _player.vc_isDisappeared = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    // step 5
    _player.vc_isDisappeared = YES;
}

模式2: 在TableView或UICollectionView中播放时, 当播放器视图滑动消失时, 显示小浮窗视图

1610601289913567.mp4
        _player = [SJVideoPlayer player];
        // step 1: 开启控制器
        _player.floatSmallViewController.enabled = YES;
        
        __weak typeof(self) _self = self;
        // step 2: 配置单击小浮窗时的回调
        _player.floatSmallViewController.singleTappedOnTheFloatViewExeBlock = ^(id<SJFloatSmallViewController>  _Nonnull floatSmallViewController) {
            __strong typeof(_self) self = _self;
            if ( !self ) return ;
            [floatSmallViewController dismissFloatView];
            [self.navigationController pushViewController:[[SJViewController4 alloc] initWithAsset:self.player.URLAsset] animated:YES];
        };
        
        // step 3: 配置双击小浮窗时的回调
        _player.floatSmallViewController.doubleTappedOnTheFloatViewExeBlock = ^(id<SJFloatSmallViewController>  _Nonnull controller) {
            __strong typeof(_self) self = _self;
            if ( !self ) return ;
            
            self.player.timeControlStatus == SJPlaybackTimeControlStatusPaused ? [self.player play] : [self.player pause];
        };
Clone this wiki locally