iOS16灵动岛横屏视频播放适配(ZFPlayer)

news/2024/7/20 22:00:25 标签: 音视频, ios, xcode

项目场景:

手机为iphone14Pro
版本iOS16.0.3
Xcode版本14.2
视频播放第三方库ZFPlayer


问题描述

使用视频时,视频播放自动横屏控制层的返回按钮和暂停按钮都点不到,上图错误、下图正确(控制按钮距离屏幕左右减小50、视频全屏不做改变)
在这里插入图片描述
在这里插入图片描述


原因分析:

  1. 全屏没有考虑灵动岛的范围,这里在屏幕旋转时重置控制层View的frame,全局持有ZFPlayerControlView控制层,将控制层左右缩小合适距离,我这里取得50

    self.controlView.frame = CGRectMake(50, 0, AKScreenHeight-100, AKScreenWidth);
    
  2. 注意在旋转回去时需要将高宽重置回去,重置相对于父容器的,所以是

    CGRectMake(0, 0, self.videoContainerView.width, self.videoContainerView.height)
    

解决方案:

关键代码:

self.playerControlView.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen){
        @strongify(self);

        dispatch_async(dispatch_get_main_queue(), ^{
            if(@available(iOS 16.0, *)){
                if(isFullScreen){
                    self.controlView.frame = CGRectMake(50, 0, AKScreenHeight-100, AKScreenWidth);
                }else{
                    self.controlView.frame = CGRectMake(0, 0, self.videoContainerView.width, self.videoContainerView.height);
                }
            }
        });

    };

全部代码:


#import "ZFAVPlayerManager.h"
#import "ZFPlayerController.h"


//===================

@property (nonatomic, strong) UIView *shadowView;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIImageView *mainImageView;

@property (nonatomic ,strong) UIView *videoContainerView;
@property (nonatomic, strong) NSString *mediaUrl;
@property (nonatomic, strong) UILabel *tips4GLabel;

@property (nonatomic, strong) ZFPlayerController *playerControlView;//视频播放器
@property (nonatomic, strong) ZFAVPlayerManager *playerManager;
@property (nonatomic, strong) ZFPlayerControlView *controlView;

//===================

- (void)showVedioView{
    
    self.mediaUrl =@"http://xxxxx.mp4";//视频
    
    self.videoContainerView = [UIView new];
    self.videoContainerView.layer.cornerRadius = 4;
    [self.view addSubview:self.videoContainerView];

    
    self.mainImageView = [UIImageView new];
    [self.view addSubview:self.mainImageView];
    self.mainImageView.layer.cornerRadius = 4;
    self.mainImageView.clipsToBounds = YES;
    self.mainImageView.userInteractionEnabled = YES;
    self.mainImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.mainImageView.hidden = NO;
    
    
    self.playerManager = [[ZFAVPlayerManager alloc] init];
    
    self.playerManager.scalingMode = ZFPlayerScalingModeAspectFit;
    /// 播放器相关
    self.playerControlView = [ZFPlayerController playerWithPlayerManager:self.playerManager containerView:self.videoContainerView];
    self.playerControlView.shouldAutoPlay = NO;
    self.playerControlView.pauseByEvent = YES;
    
    ZFPlayerControlView *controlView = [ZFPlayerControlView new];
    controlView.fastViewAnimated = YES;
    controlView.prepareShowLoading = YES;

    self.playerControlView.controlView = controlView;
    self.controlView = controlView;
    
    UIView *shadowView = [UIView new];
    shadowView.userInteractionEnabled = YES;
    [self.mainImageView addSubview:shadowView];
    shadowView.backgroundColor = ColorHexAlpha(@"#000000", 0.4);
    
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.mainImageView addSubview:self.playButton];
    [self.playButton setImage:[UIImage imageNamed:@"vedio_start_icon"] forState:UIControlStateNormal];
    
    @weakify(self);
    [[self.playButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        self.playerControlView.assetURL = [NSURL URLWithString:self.mediaUrl];
        self.mainImageView.hidden = YES;
        self.videoContainerView.hidden = NO;
        self.playButton.hidden = YES;
        
        [self.playerControlView.currentPlayerManager play];
        self.playerControlView.playerLoadStateChanged = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset, ZFPlayerLoadState loadState) {
        };
        
        
    }];
    
    
    self.playerControlView.playerPlayTimeChanged = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset, NSTimeInterval currentTime, NSTimeInterval duration) {
        @strongify(self);
        
    };
    
    
    [self.playerControlView setPlayerDidToEnd:^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
        @strongify(self);
        self.mainImageView.hidden = NO;
        self.playButton.hidden = NO;
        self.videoContainerView.hidden = YES;
    }];
    
    self.playerControlView.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen){
        @strongify(self);

        dispatch_async(dispatch_get_main_queue(), ^{
            if(@available(iOS 16.0, *)){
                if(isFullScreen){
                    self.controlView.frame = CGRectMake(50, 0, AKScreenHeight-100, AKScreenWidth);
                }else{
                    self.controlView.frame = CGRectMake(0, 0, self.videoContainerView.width, self.videoContainerView.height);
                }
            }
        });

    };

   
    
    [self.videoContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@16);
        make.right.equalTo(@(-16));
        make.top.equalTo(self.view).offset(323*FTGetScreenScale());
        make.height.offset(175*FTGetScreenScale());
    }];

    [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self.videoContainerView);
    }];
    
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(@0);
    }];
    
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mainImageView);
        make.top.equalTo(self.mainImageView.mas_top).offset(85*FTGetScreenScale());
        make.width.equalTo(@(92*FTGetScreenScale()));
        make.height.equalTo(@(23*FTGetScreenScale()));
    }];
    
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(61, 432, 252, 20);
    [self.mainImageView addSubview:label];
    NSMutableAttributedString *string = [
      [NSMutableAttributedString alloc] initWithString:@"当前为非无线网络环境,请注意流量消耗"
      attributes: @{
        NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Medium" size: 14*FTGetScreenScale()],
        NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.00],
    }];
    label.attributedText = string;
    label.textAlignment = NSTextAlignmentLeft;
    self.tips4GLabel = label;
    NSInteger status = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).networkStatus;
	 if (status == 1) {
        // 流量提示
        self.tips4GLabel.hidden = NO;
        self.playButton.hidden = NO;
    }else{
        self.tips4GLabel.hidden = YES;
        self.playerControlView.assetURL = [NSURL URLWithString:self.mediaUrl];
        self.mainImageView.hidden = YES;
        self.videoContainerView.hidden = NO;
        self.playButton.hidden = YES;
        
        [self.playerControlView.currentPlayerManager play];
    }
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mainImageView);
        make.top.equalTo(self.mainImageView.mas_top).offset(52.5*FTGetScreenScale());
        make.width.equalTo(@(252*FTGetScreenScale()));
        make.height.equalTo(@(20*FTGetScreenScale()));
    }];
    
}

oc版本的,Swift请自己再转一下。也有新push到另一个控制层进行播放的,因为需要小屏和全屏播放更丝滑一点,没有采用另一个方案.


http://www.niftyadmin.cn/n/51108.html

相关文章

国产台灯哪个品牌比较好?推荐高品质的台灯品牌

台灯成为每个家庭都用到的小家电&#xff0c;随着时代的进步&#xff0c;国内也出现许多做灯具的品牌&#xff0c;在国产台灯中哪个品牌比较好呢&#xff1f;分享几款好用的护眼台灯品牌。1000元以上高品质的护眼台灯推荐&#xff1a;NANK南卡&#xff08;一&#xff09;型号&a…

【SQL开发实战技巧】系列(八):聊聊如何插入数据时比约束更灵活的限制数据插入以及怎么一个insert语句同时插入多张表

系列文章目录 【SQL开发实战技巧】系列&#xff08;一&#xff09;:关于SQL不得不说的那些事 【SQL开发实战技巧】系列&#xff08;二&#xff09;&#xff1a;简单单表查询 【SQL开发实战技巧】系列&#xff08;三&#xff09;&#xff1a;SQL排序的那些事 【SQL开发实战技巧…

day 15 第六章二叉树

层序遍历 102.二叉树的层序遍历107.二叉树的层次遍历II199.二叉树的右视图637.二叉树的层平均值429.N叉树的层序遍历515.在每个树行中找最大值116.填充每个节点的下一个右侧节点指针117.填充每个节点的下一个右侧节点指针II104.二叉树的最大深度111.二叉树的最小深度 226.翻转二…

nodejs基于vue的饭店点餐外卖平台网站

本系统主要实现了管理员&#xff1a;首页、个人中心、用户管理、菜品分类管理、菜品信息管理、菜品评价管理、系统管理、订单管理,用户&#xff1a;首页、个人中心、菜品评价管理、我的收藏管理、订单管理,前台首页&#xff1a;首页、菜品信息、菜品资讯、个人中心、后台管理、…

Spring面试重点(一)——Spring容器

Spring容器 手写Autowired注解 /** 实现spring的Autowired注解 **/ //运行时触发 Retention(RetentionPolicy.RUNTIME) //作用于变量 Target(ElementType.FIELD) public interface Autowired {}public class UserController {Autowiredprivate UserService userService;/*** …

论文文献引用规范和标准(国标GBT7714)@endnote国标样式

文章目录论文文献引用规范和标准&#xff08;国标GBT7714&#xff09;国标GBT7714-2015endnote stylerefs简述国标GBT7714条目的组织格式Noteword中的文献交叉引用超链接文献引用示例endNote资源和基本使用endnote或其他文献引用工具下载word中的其他引文技巧知网国标格式引文引…

2023-02-09 Elasticsearch 模糊搜索

1 prefix 前缀搜索 以前缀开头的搜索&#xff0c;不计算相关度得分 前缀搜索匹配的是term&#xff0c;而不是field。 前缀搜索的性能很差 前缀搜索没有缓存 前缀搜索尽可能把前缀长度设置的更长 针对于中文分词器 index_prefixes: 默认 “min_chars” : 2, “max_chars” : …

虹科分享|在ntopng中使用多用户模式

并非所有 ntop 用户都知道 ntopng 本机实现了多用户支持。也就是说&#xff0c;您可以使用ntopng收集和分析来自多个用户的流量&#xff0c;并向每个用户显示自己的流量&#xff0c;隐藏其余所有流量。 您需要做的就是非常简单&#xff1a; 1. 启动 ntopng 并将其配置为接收受…