iOS画中画功能

一、使用前

1、头文件

import <AVKit/AVKit>   
我们会用到  AVPictureInPictureController对应的画中画功能

2、配置

一般我们用画中画功能主要是后台使用,需要添加后台功能

设置音频后台播放

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];

、设置会话激活

二、API介绍

AVPictureInPictureControllerNSObject的一个子类,可用于在应用程序的顶部呈现AVPlayerLayerAVPlayerView的内容。

1、属性和方法

获取是否支持画中画功能

/*!
	@method		isPictureInPictureSupported
	@abstract	返回当前设备是否支持画中画功能
	@discussion	如果不支持,所有的初始化结果都会是nil
 */
+ (BOOL)isPictureInPictureSupported;

系统默认的画中画按钮图标

/*! 开始图标
	@method		pictureInPictureButtonStartImageCompatibleWithTraitCollection:
	@param		traitCollection 要检索的图像特征,传nil表示主屏幕特征.
	@abstract   系统默认的画中画开始模板图片对应客户端要使用的画中画按钮
 */
+ (UIImage *)pictureInPictureButtonStartImageCompatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection;

// 13后添加的属性,同上边的方法
@property (class, nonatomic, readonly) UIImage *pictureInPictureButtonStartImage API_AVAILABLE(ios(13.0), tvos(14.0));


/*! 结束图标
	@method		pictureInPictureButtonStopImageCompatibleWithTraitCollection:
	@param		traitCollection
				要检索的图像特征,传nil表示主屏幕特征
	@abstract	   系统默认的画中画结束模板图片对应客户端要使用的画中画按钮
 */
+ (UIImage *)pictureInPictureButtonStopImageCompatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection;

// 13后添加的属性,同上边的方法
@property (class, nonatomic, readonly) UIImage *pictureInPictureButtonStopImage API_AVAILABLE(ios(13.0), tvos(14.0));

添加播放器

/*!
 @method		initWithPlayerLayer:
 @param			playerLayer
 				The player layer from which to source the media content for the Picture in Picture controller.
 @abstract		Designated initializer.
 */
- (nullable instancetype)initWithPlayerLayer:(AVPlayerLayer *)playerLayer NS_DESIGNATED_INITIALIZER;

/*!
	@property	playerLayer
	@abstract	The receiver's player layer.
 */
@property (nonatomic, readonly) AVPlayerLayer *playerLayer;

画中画方法

/*!
	@method		startPictureInPicture
	@abstract 如果可以的话,为提供的AVPlayerLayer开始画中画
	@discussion	如果当前可以画中画,接收方将调用回调方法
	-pictureInPictureControllerWillStartPictureInPicture:
	并在成功之后调用
	-pictureInPictureControllerDidStartPictureInPicture:
	如果画中画启动失败 将会调用
	-pictureInPictureControllerFailedToStartPictureInPicture:withError:
	客户端可以通过-stopPictureInPicture 停止画中画功能,此外还可以在画中画中,通过用户交互停止。
	在停止之前,将调用
	-pictureInPictureControllerWillStopPictureInPicture:
	和停止后调用
	pictureInPictureControllerDidStopPictureInPicture:
 */
- (void)startPictureInPicture;

/*!
	@method		stopPictureInPicture
	@abstract   如果当前正在活跃状态,停止画中画。在tvOS上,这也可以停止其他应用程序的画中画会话
 */
- (void)stopPictureInPicture;

状态判断

/*!
	@property	pictureInPicturePossible
	@abstract   是否当前的画中画功能可用
 */
@property (nonatomic, readonly, getter = isPictureInPicturePossible) BOOL pictureInPicturePossible;

/*!
	@property	pictureInPictureActive
	@abstract   是否当前的画中画处于活跃状态
 */
@property (nonatomic, readonly, getter = isPictureInPictureActive) BOOL pictureInPictureActive;

/*!
	@property	pictureInPictureSuspended
	@abstract   是否当前的画中画处于暂停状态
 */
@property (nonatomic, readonly, getter = isPictureInPictureSuspended) BOOL pictureInPictureSuspended;

/*!
	@property	canStopPictureInPicture
	@abstract  是否当前有活跃的画中画并且可用停止
	@discussion 当为true时,stopPictureInPicture将停止当前活动的会话,当这个属性改变时,应用应该重新检索系统提供的画中画开始图片
 */
@property (nonatomic, readonly) BOOL canStopPictureInPicture API_AVAILABLE(tvos(14.0)) API_UNAVAILABLE(ios, macos, watchos);

/*!
	@property	requiresLinearPlayback
	@abstract  禁用某些用户操作(快进、前进跳跃)。
	@discussion	这个可以用来临时强制播放一些内容,如法律术语或广告
 */
@property (nonatomic) BOOL requiresLinearPlayback API_AVAILABLE(ios(14.0), macos(11.0), tvos(14.0)) API_UNAVAILABLE(watchos);

2、代理方法

将要开始画中画功能

/*! 
	@method		pictureInPictureControllerWillStartPictureInPicture:
	@abstract	代理实现该方法,便于在画面将启动时得到通知.
 */
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;

已经开始画中画功能

/*! 
	@method		pictureInPictureControllerDidStartPictureInPicture:
	@abstract	代理实现该方法,得到画中华已经启动的通知.
 */
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;

启动失败

/*!
	@method		pictureInPictureController:failedToStartPictureInPictureWithError:
	@param		error 错误描述如果失败的话
	@abstract	代理实现这个方法,得到画中画启动失败的通知
 */
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error;

将要停止

/*!
	@method		pictureInPictureControllerWillStopPictureInPicture:
	@abstract	代理实现这个方法,得到画中画将要停止的通知
 */
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;

已经停止

/*!
	@method		pictureInPictureControllerDidStopPictureInPicture:
	@abstract	代理实现这个方法,得到画中画已经停止的通知
 */
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;

完成回调

/*!
	@method		pictureInPictureController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:
	@param		completionHandler 代理在恢复后需要调用的完成回调.
	@abstract	代理可以实现这个方法,在画中画将要结束前恢复用户交互
 */
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;

三、结语

将api简单的试用了一下,iOS14真机可以实现后台画中画功能,模拟器possible方法返回失败,启动也无任何反应

demo地址

posted @ 2020-11-30 23:35  absty_guo  阅读(3236)  评论(0编辑  收藏  举报