在iOS開發框架中,AVKit是一個很是上層,偏應用的框架,它是基於AVFoundation的一層視圖層封裝。其中相關文件和類都十分簡單,本篇博客主要整理和總結AVKit中相關類的使用方法。ios
AVRoutePickerView是iOS 11後新加入的類,AirPlay是iOS設備方便用戶使用的一大特色。其做用是將當前手機播放的音頻或者視頻投送到其餘外部設備上,例如支持AirPlay的電視,車載設備等。AVRoutePickerView只是一個按鈕,其用來方便用戶能夠直接在應用程序內喚出AirPlay選擇窗口。示例以下:框架
- (void)viewDidLoad { [super viewDidLoad]; AVRoutePickerView * view = [[AVRoutePickerView alloc]initWithFrame:CGRectMake(100, 100, 60, 60)]; //活躍狀態顏色 view.activeTintColor = [UIColor redColor]; //設置代理 view.delegate = self; [self.view addSubview:view]; } //AirPlay界面彈出時回調 - (void)routePickerViewWillBeginPresentingRoutes:(AVRoutePickerView *)routePickerView{ NSLog(@"!!!!!!!!"); } //AirPlay界面結束時回調 - (void)routePickerViewDidEndPresentingRoutes:(AVRoutePickerView *)routePickerView{ NSLog(@"@@@@@@@@"); }
按鈕和彈出界面效果以下:ide
從上面的示例代碼也能夠看出,對於AVRoutePickerView,咱們基本沒有任何能夠進行自定義的餘地,從UI效果到按鈕的觸發方法所有由AVKit封裝好了,它只是一個喚出系統功能的接口。atom
AVPlayerViewController是對AVFoundation中的AVPlayer與AVPlayerLayer的封裝,它是一個封裝好的視圖控制器,包含了視頻的播放和控制功能。這個類在iOS8以後可用,解析以下:spa
@interface AVPlayerViewController : UIViewController //視頻播放器對象 @property (nonatomic, strong, nullable) AVPlayer *player; //是否顯示視頻播放控制組件 @property (nonatomic) BOOL showsPlaybackControls; //設置視頻的填充方式 /* //按比例縮放 AVF_EXPORT AVLayerVideoGravity const AVLayerVideoGravityResizeAspect NS_AVAILABLE(10_7, 4_0); //按比例填充 AVF_EXPORT AVLayerVideoGravity const AVLayerVideoGravityResizeAspectFill NS_AVAILABLE(10_7, 4_0); //充滿 AVF_EXPORT AVLayerVideoGravity const AVLayerVideoGravityResize NS_AVAILABLE(10_7, 4_0); */ @property (nonatomic, copy) AVLayerVideoGravity videoGravity; //視頻的第一幀是否已經準備好了 @property (nonatomic, readonly, getter = isReadyForDisplay) BOOL readyForDisplay; //獲取視頻的尺寸 @property (nonatomic, readonly) CGRect videoBounds; //內容覆蓋層 能夠向其上添加子視圖 會出如今視頻層與控制層之間 @property (nonatomic, readonly, nullable) UIView *contentOverlayView; //是否容許畫中畫 iOS9以上可用 ipad可用 @property (nonatomic) BOOL allowsPictureInPicturePlayback API_AVAILABLE(ios(9.0)); //是否對信息中心的播放器信息進行更新 默認爲YES @property (nonatomic) BOOL updatesNowPlayingInfoCenter API_AVAILABLE(ios(10.0)); //是否默認進行全屏播放 @property (nonatomic) BOOL entersFullScreenWhenPlaybackBegins API_AVAILABLE(ios(11.0)); //播放結束後 是否默認退出全屏 @property (nonatomic) BOOL exitsFullScreenWhenPlaybackEnds API_AVAILABLE(ios(11.0)); //代理 @property (nonatomic, weak, nullable) id <AVPlayerViewControllerDelegate> delegate API_AVAILABLE(ios(9.0)); @end
AVPlayerViewControllerDelegate解析以下:代理
//將要開始畫中畫時調用 - (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController; //已經開始畫中畫時調用 - (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController; //開啓畫中畫失敗調用 - (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error; //將要結束畫中畫調用 - (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController; //已經結束畫中畫調用 - (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController; //是否自動關閉控制器當畫中畫開始時 - (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController; //畫中畫結束後回覆以前的用戶界面 - (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;
AVPictureInPictureController是一個控制器,用來對畫中畫進行相關操做,解析以下:rest
@interface AVPictureInPictureController : NSObject //獲取當前設備是否支持畫中畫 + (BOOL)isPictureInPictureSupported; //畫中畫轉換開始按鈕圖像 + (UIImage *)pictureInPictureButtonStartImageCompatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection; //畫中畫轉換結束按鈕圖像 + (UIImage *)pictureInPictureButtonStopImageCompatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection; //構造方法 - (nullable instancetype)initWithPlayerLayer:(AVPlayerLayer *)playerLayer; //播放器視圖 @property (nonatomic, readonly) AVPlayerLayer *playerLayer; //代理 @property (nonatomic, weak, nullable) id <AVPictureInPictureControllerDelegate> delegate; //開始畫中畫 - (void)startPictureInPicture; //結束畫中畫 - (void)stopPictureInPicture; //畫中畫目前是否可用 @property (nonatomic, readonly, getter = isPictureInPicturePossible) BOOL pictureInPicturePossible; //畫中畫是否激活 @property (nonatomic, readonly, getter = isPictureInPictureActive) BOOL pictureInPictureActive; //是否支持畫中畫 @property (nonatomic, readonly, getter = isPictureInPictureSuspended) BOOL pictureInPictureSuspended;