目錄
一、使用前
1、頭文件
import <AVKit/AVKit>
我們會用到 AVPictureInPictureController對應的畫中畫功能
2、配置
一般我們用畫中畫功能主要是后台使用,需要添加后台功能
設置音頻后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
、設置會話激活
二、API介紹
AVPictureInPictureController
是NSObject
的一個子類,可用於在應用程序的頂部呈現AVPlayerLayer
或AVPlayerView
的內容。
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方法返回失敗,啟動也無任何反應