iOS开发之AVKit框架使用
一、引言
在iOS开发框架中,AVKit是一个非常上层,偏应用的框架,它是基于AVFoundation的一层视图层封装。其中相关文件和类都十分简单,本篇博客主要整理和总结AVKit中相关类的使用方法。
二、AVRoutePickerView
AVRoutePickerView是iOS 11后新加入的类,AirPlay是iOS设备方便用户使用的一大特点。其作用是将当前手机播放的音频或者视频投送到其他外部设备上,例如支持AirPlay的电视,车载设备等。AVRoutePickerView只是一个按钮,其用来方便用户可以直接在应用程序内唤出AirPlay选择窗口。示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| - (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]; }
- (void)routePickerViewWillBeginPresentingRoutes:(AVRoutePickerView *)routePickerView{ NSLog(@"!!!!!!!!"); }
- (void)routePickerViewDidEndPresentingRoutes:(AVRoutePickerView *)routePickerView{ NSLog(@"@@@@@@@@"); }
|
按钮和弹出界面效果如下:
从上面的示例代码也可以看出,对于AVRoutePickerView,我们基本没有任何可以进行自定义的余地,从UI效果到按钮的触发方法全部由AVKit封装好了,它只是一个唤出系统功能的接口。
三、AVPlayerViewController
AVPlayerViewController是对AVFoundation中的AVPlayer与AVPlayerLayer的封装,它是一个封装好的视图控制器,包含了视频的播放和控制功能。这个类在iOS8之后可用,解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @interface AVPlayerViewController : UIViewController
@property (nonatomic, strong, nullable) AVPlayer *player;
@property (nonatomic) BOOL showsPlaybackControls;
@property (nonatomic, copy) AVLayerVideoGravity videoGravity;
@property (nonatomic, readonly, getter = isReadyForDisplay) BOOL readyForDisplay;
@property (nonatomic, readonly) CGRect videoBounds;
@property (nonatomic, readonly, nullable) UIView *contentOverlayView;
@property (nonatomic) BOOL allowsPictureInPicturePlayback API_AVAILABLE(ios(9.0));
@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解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| - (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
AVPictureInPictureController是一个控制器,用来对画中画进行相关操作,解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @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;
|