版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.12.30 |
前言
iOS系统中有很多方式可以播放视频文件,这里我们就详细的说明下播放视频文件的原理和实例。希望能帮助到大家,大家能喜欢。感兴趣的可以参考上面几篇。
1. 几种播放视频文件的方式(一) —— 总结播放视频的几种方式(一)
2. 几种播放视频文件的方式(二) —— 基于MediaPlayer框架的视频播放(一)
3. 几种播放视频文件的方式(三) —— 基于AVFoundation框架视频播放(一)
AVKit框架
1. 框架API
我们还是先看一下AVKit的API
/*
File: AVKit.h
Framework: AVKit
Copyright © 2013-2016 Apple Inc. All rights reserved.
To report bugs, go to: http://developer.apple.com/bugreporter/
*/
#import
#if TARGET_OS_IPHONE
#import
#import
#import
#import
#else
#import
#import
#endif // TARGET_OS_IPHONE
2. AVPlayerViewController
下面我们看一下该类的API
/*
File: AVPlayerViewController.h
Framework: AVKit
Copyright © 2014-2017 Apple Inc. All rights reserved.
*/
#import
#import
NS_ASSUME_NONNULL_BEGIN
@protocol AVPlayerViewControllerDelegate;
/*!
@class AVPlayerViewController
@abstract AVPlayerViewController is a subclass of UIViewController that can be used to display the visual content of an AVPlayer object and the standard playback controls.
*/
API_AVAILABLE(ios(8.0))
@interface AVPlayerViewController : UIViewController
/*!
@property player
@abstract The player from which to source the media content for the view controller.
*/
@property (nonatomic, strong, nullable) AVPlayer *player;
/*!
@property showsPlaybackControls
@abstract Whether or not the receiver shows playback controls. Default is YES.
@discussion Clients can set this property to NO when they don't want to have any playback controls on top of the visual content (e.g. for a game splash screen).
*/
@property (nonatomic) BOOL showsPlaybackControls;
/*!
@property videoGravity
@abstract A string defining how the video is displayed within an AVPlayerLayer bounds rect.
@discussion Options are AVLayerVideoGravityResizeAspect, AVLayerVideoGravityResizeAspectFill and AVLayerVideoGravityResize. AVLayerVideoGravityResizeAspect is default.
See for a description of these options.
*/
@property (nonatomic, copy) NSString *videoGravity;
/*!
@property readyForDisplay
@abstract Boolean indicating that the first video frame has been made ready for display for the current item of the associated AVPlayer.
*/
@property (nonatomic, readonly, getter = isReadyForDisplay) BOOL readyForDisplay;
/*!
@property videoBounds
@abstract The current size and position of the video image as displayed within the receiver's view's bounds.
*/
@property (nonatomic, readonly) CGRect videoBounds;
/*!
@property contentOverlayView
@abstract Use the content overlay view to add additional custom views between the video content and the controls.
*/
@property (nonatomic, readonly, nullable) UIView *contentOverlayView;
/*!
@property allowsPictureInPicturePlayback
@abstract Whether or not the receiver allows Picture in Picture playback. Default is YES.
*/
@property (nonatomic) BOOL allowsPictureInPicturePlayback API_AVAILABLE(ios(9.0));
/*!
@property updatesNowPlayingInfoCenter
@abstract Whether or not the now playing info center should be updated. Default is YES.
*/
@property (nonatomic) BOOL updatesNowPlayingInfoCenter API_AVAILABLE(ios(10.0));
/*!
@property entersFullScreenWhenPlaybackBegins
@abstract Whether or not the receiver automatically enters full screen when the play button is tapped. Default is NO.
@discussion If YES, the receiver will show a user interface tailored to this behavior.
*/
@property (nonatomic) BOOL entersFullScreenWhenPlaybackBegins API_AVAILABLE(ios(11.0));
/*!
@property exitsFullScreenWhenPlaybackEnds
@abstract Whether or not the receiver automatically exits full screen when playback ends. Default is NO.
@discussion If multiple player items have been enqueued, the receiver exits fullscreen once no more items are remaining in the queue.
*/
@property (nonatomic) BOOL exitsFullScreenWhenPlaybackEnds API_AVAILABLE(ios(11.0));
/*!
@property delegate
@abstract The receiver's delegate.
*/
@property (nonatomic, weak, nullable) id delegate API_AVAILABLE(ios(9.0));
@end
/*!
@protocol AVPlayerViewControllerDelegate
@abstract A protocol for delegates of AVPlayerViewController.
*/
@protocol AVPlayerViewControllerDelegate
@optional
/*!
@method playerViewControllerWillStartPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture will start.
*/
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;
/*!
@method playerViewControllerDidStartPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture did start.
*/
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;
/*!
@method playerViewController:failedToStartPictureInPictureWithError:
@param playerViewController
The player view controller.
@param error
An error describing why it failed.
@abstract Delegate can implement this method to be notified when Picture in Picture failed to start.
*/
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;
/*!
@method playerViewControllerWillStopPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture will stop.
*/
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;
/*!
@method playerViewControllerDidStopPictureInPicture:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method to be notified when Picture in Picture did stop.
*/
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;
/*!
@method playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:
@param playerViewController
The player view controller.
@abstract Delegate can implement this method and return NO to prevent player view controller from automatically being dismissed when Picture in Picture starts.
*/
- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController;
/*!
@method playerViewController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:
@param playerViewController
The player view controller.
@param completionHandler
The completion handler the delegate needs to call after restore.
@abstract Delegate can implement this method to restore the user interface before Picture in Picture stops.
*/
- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;
@end
NS_ASSUME_NONNULL_END
功能实现
下面我们看一下功能实现。
#import "ViewController.h"
#import
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerViewController *playerVC;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//如果没有声音,需要加上这一句话
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
AVPlayer *player = [AVPlayer playerWithURL:[[NSBundle mainBundle] URLForResource:@"movie.mp4" withExtension:nil]];
self.player = player;
AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
self.playerVC = playerVC;
playerVC.player = player;
playerVC.view.frame = self.view.frame;
playerVC.delegate = self;
// 设置拉伸模式
playerVC.videoGravity = AVLayerVideoGravityResizeAspect;
// 设置是否显示媒体播放组件
playerVC.showsPlaybackControls = YES;
[self.view addSubview:playerVC.view];
[self addChildViewController:playerVC];
//这个属性和图片填充视图的属性类似,也可以设置为自适应试图大小。
player.externalPlaybackVideoGravity = AVLayerVideoGravityResizeAspectFill;
[player play];
}
#pragma mark - AVPlayerViewControllerDelegate
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController
{
NSLog(@"视频已经开始的时候调用");
}
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error
{
NSLog(@"播放失败的时候");
}
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController
{
NSLog(@"将要停止的时候");
}
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController
{
NSLog(@"已经停止的时候");
}
@end
下面看一下实现效果
后记
未完,待续~~~