iOS開發(fā)-iOS14畫中畫(OC)

一 簡述

一種控制器,用于在浮動的可調(diào)整大小的窗口中響應用戶啟動的畫中畫視頻播放。

API_AVAILABLE(ios(9.0), macos(10.15), tvos(14.0)) API_UNAVAILABLE(watchos)
@interface AVPictureInPictureController : NSObject

注意

畫中畫(PiP)是Apple希望始終在用戶控制下的一項用戶功能。僅在響應用戶的明確請求時才調(diào)用PiP。如果某個應用以非用戶直接指導的方式調(diào)用PiP,則App Store審核小組將拒絕它。

要在iOS中使用畫中畫,要在Xcode中執(zhí)行以下步驟:

  1. 打開后臺模式開啟 Audio, AirPlay, and Picture in Picture
image-20201111164716519
  1. 為音頻會話配置合適的類別,如AVAudioSessionCategoryPlayback 、AVAudioSessionCategoryPlayAndRecord

重要

不支持子類化和重寫其方法,這會導致未定義的行為。

二 官方屬性方法

// 當前設(shè)備是否支持畫中畫
+ (BOOL)isPictureInPictureSupported;
// 創(chuàng)建畫中畫控制器
- (nullable instancetype)initWithPlayerLayer:(AVPlayerLayer *)playerLayer NS_DESIGNATED_INITIALIZER;
// 要播放媒體的播放器層
@property (nonatomic, readonly) AVPlayerLayer *playerLayer;
// 是否允許用戶跳過媒體內(nèi)容
@property (nonatomic) BOOL requiresLinearPlayback API_AVAILABLE(ios(14.0), macos(11.0), tvos(14.0)) API_UNAVAILABLE(watchos);
// 委托對象
@property (nonatomic, weak, nullable) id <AVPictureInPictureControllerDelegate> delegate;
// 當前是否可以進行畫中畫回放
@property (nonatomic, readonly, getter = isPictureInPicturePossible) BOOL pictureInPicturePossible;
// 控制器的畫中畫窗口是否在屏幕上
@property (nonatomic, readonly, getter = isPictureInPictureActive) BOOL pictureInPictureActive;
// 系統(tǒng)是否掛起控制器的畫中畫窗口
@property (nonatomic, readonly, getter = isPictureInPictureSuspended) BOOL pictureInPictureSuspended;

// 開始播放
- (void)startPictureInPicture;
// 停止播放(如果當前處于活躍狀態(tài))
- (void)stopPictureInPicture;
// 是否處于活躍狀態(tài)并且可以關(guān)閉
@property (nonatomic, readonly) BOOL canStopPictureInPicture API_AVAILABLE(tvos(14.0)) API_UNAVAILABLE(ios, macos, watchos);

// 系統(tǒng)默認的“畫中畫”開始模板圖像,用于應用程序的“畫中畫”按鈕。
@property (class, nonatomic, readonly) UIImage *pictureInPictureButtonStartImage API_AVAILABLE(ios(13.0), tvos(14.0));
// 系統(tǒng)默認的畫中畫停止模板圖像,用于應用程序的畫中畫按鈕。
@property (class, nonatomic, readonly) UIImage *pictureInPictureButtonStopImage API_AVAILABLE(ios(13.0), tvos(14.0));

// 告訴委托人畫中畫即將停止的時間,使您的應用有機會恢復其視頻播放用戶界面。
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;
// 當畫中畫即將開始時
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
// 該畫中畫回放已經(jīng)開始
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
// 畫中畫無法啟動
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error;
// 畫中畫即將停止
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
// 畫中畫停止
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;

三 簡單示例

1. 導入框架`#import <AVKit/AVKit.h>`

2. 創(chuàng)建畫中畫對象

//1.判斷是否支持畫中畫功能
if ([AVPictureInPictureController isPictureInPictureSupported]) {
    //2.開啟權(quán)限
    @try {
        NSError *error = nil;
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
        [[AVAudioSession sharedInstance] setActive:YES error:&error];
    } @catch (NSException *exception) {
        NSLog(@"AVAudioSession錯誤");
    }
    self.pip = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.player];
    self.pip.delegate = self;
}

注意:上述代碼里的self.player必須是AVPlayerLayer類型。

3. 開啟/關(guān)閉

if (self.pip.isPictureInPictureActive) {
    [self.pip stopPictureInPicture];
} else {
    [self.pip startPictureInPicture];
}

4. 實現(xiàn)AVPictureInPictureControllerDelegate代理

// 即將開啟畫中畫
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {

}
// 已經(jīng)開啟畫中畫
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {

}
// 開啟畫中畫失敗
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {

}
// 即將關(guān)閉畫中畫
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {

}
// 已經(jīng)關(guān)閉畫中畫
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {

}
// 關(guān)閉畫中畫且恢復播放界面
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容