AVPlayer是一个可以播放任何格式的全功能影音播放器,使用AVPlayer需导入AVFoundation.h。
在开发中,单纯使用AVPlayer类是无法显示视频的,要将视频层添加至AVPlayerLayer中,这样才能将视频显示出来。
AVPlayer并未提供视频操作组件,需用户自定义。
+ (instancetype)playerWithURL:(NSURL *)URL;
+ (instancetype)playerWithPlayerItem:(AVPlayerItem *)item;
- (instancetype)initWithURL:(NSURL *)URL;
- (instancetype)initWithPlayerItem:(AVPlayerItem *)item;
1、初始化方法
// 1、遍历初始化
+ (instancetype)layer;
// 2、alloc + init
// 3、根据播放媒体初始化
+ (AVPlayerLayer *)playerLayerWithPlayer:(nullable AVPlayer *)player;
2、常用属性
bounds:设置播放矩形区域;
position:设置播放区域中心点位置;
videoGravity:设置拉伸模式
player:设置播放媒体
三、AVPlayerViewController
使用
iOS9以后支持画中画;
// 1、获取本地资源地址
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"Cat.mp4"]];
url = urlBundle;
// 2、初始化媒体播放控制器
if (_playerViewController) {
_playerViewController = nil;
}
// 3、配置媒体播放控制器
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.delegate = self;
// _playerViewController.showsPlaybackControls = NO;
// _playerViewController.view.userInteractionEnabled = NO;
// 设置媒体源数据
_playerViewController.player = [AVPlayer playerWithURL:url];
// 设置拉伸模式
_playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
// 设置是否显示媒体播放组件
// 播放视频
[_playerViewController.player play];
// 设置媒体播放器视图大小
_playerViewController.view.bounds = CGRectMake(0, 100, 320, 400);
_playerViewController.view.center = self.view.center;
// 推送至媒体播放器进行播放
// [self presentViewController:_playerViewController animated:YES completion:nil];
// 直接在本视图控制器播放
[self addChildViewController:_playerViewController];
[self.view addSubview:_playerViewController.view];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
属性说明
@interface AVPlayerViewController : UIViewController
@property (nonatomic, strong, nullable) AVPlayer *player;
@property (nonatomic) BOOL showsPlaybackControls;
@property (nonatomic, copy) NSString *videoGravity;
@property (nonatomic, readonly, getter = isReadyForDisplay) BOOL readyForDisplay;
@property (nonatomic, readonly) CGRect videoBounds;
//定制自己的展示View
@property (nonatomic, readonly, nullable) UIView *contentOverlayView;
@property (nonatomic) BOOL allowsPictureInPicturePlayback NS_AVAILABLE_IOS(9_0);
@property (nonatomic) BOOL updatesNowPlayingInfoCenter NS_AVAILABLE_IOS(10_0);
@property (nonatomic, weak, nullable) id <AVPlayerViewControllerDelegate> delegate NS_AVAILABLE_IOS(9_0);
四、视频状态监测
1、AVPlayer 与 AVPlayerItem都有status属性,可以用KVO监测;
//loadedTimeRange属性代表已经缓冲的进度,监听此属性可以在UI中更新缓冲进度,也是很有用的一个属性。
//AVPlayerItemStatus是代表当前播放资源item 的状态(可以理解成这url链接or视频文件。可以播放成功/失败)
typedef NS_ENUM(NSInteger, AVPlayerItemStatus) {
AVPlayerItemStatusUnknown,
AVPlayerItemStatusReadyToPlay,
AVPlayerItemStatusFailed
};
//AVPlayerStatus是代表当前播放器的状态。
typedef NS_ENUM(NSInteger, AVPlayerStatus) {
AVPlayerStatusUnknown,
AVPlayerStatusReadyToPlay,
AVPlayerStatusFailed
};
//编程的时候最好使用item 的status,会准确点。
2、视频跳跃播放
视频跳跃播放采用AVPlayer 对象的seekToTime:方法;
//跳到指定时间点播放
//(_palyer 为AVPlayer 对象),10是秒数,1是单位秒
[_player seekToTime:CMTimeMake(10,1)];
//播放完成回到开始位置
[_player seekToTime:kCMTimeZero];
3、AVPayerIterm的属性的监听
#pragma mark - KVO
- (void)addItemKVO
{
//KVO
[_item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[_item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
[_item addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)removeItemKVO {
[_item removeObserver:self forKeyPath:@"status"];
[_item removeObserver:self forKeyPath:@"loadedTimeRanges"];
[_item removeObserver:self forKeyPath:@"playbackBufferEmpty"];
}
#pragma mark - Notic
- (void)addVideoNotic {
//Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieJumped:) name:AVPlayerItemTimeJumpedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieStalle:) name:AVPlayerItemPlaybackStalledNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backGroundPauseMoive) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)removeVideoNotic {
//移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemPlaybackStalledNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemTimeJumpedNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}