AVPlayerViewController
使用方便但不夠靈活宗弯。
搜索引擎了半天并沒有找到自定義工具欄的方法。于是老老實實自己實現(xiàn)蒙保。
工具欄的自定義
基于AVplayer邓厕,自定義UIView,用于Player以及工具欄的顯示
@interface WKPlayerView : UIView
@property(nonatomic, strong)AVPlayer *player;
@property(nonatomic, strong)AVPlayerLayer *playerLayer;
@property(nonatomic, strong)UIControl *contentView;//用于自定義工具欄昧互,手勢事件等叽掘。
@end
在這里,我刪除了具體的界面布局代碼玖雁。用戶期望的自定義界面或事件都在contentView上實現(xiàn)即可更扁。
實現(xiàn)方式:
_contentView = [UIControl new];
[self addSubview:_contentView];
[_contentView mas_makeConstraints:^(MASConstraintMaker *make){
make.size.equalTo(self);
make.center.equalTo(self);
}];
_player = [AVPlayer playerWithURL:[NSURL URLWithString:URLString]];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
[_playerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
//將playerLayer添加到contentView下,以免contentView的事件被擋住赫冬。
[self.layer insertSublayer:_playerLayer below:_contentView.layer];
[_player play];
界面部分就完成了浓镜。
播放的自定義
播放過程中,我們可能會有播放劲厌、暫停膛薛、跳轉、獲取進度补鼻、緩沖等等事件相叁。AVPlayer提供了
- (void)play;
- (void)pause;
- (void)seekToTime:(CMTime)time;
(還有更多實現(xiàn))
- (CMTime)currentTime;
等api,可以靈活的根據需求自定義實現(xiàn)辽幌。
在這里記錄一下觀察播放進度和緩沖進度的方法:
播放進度:- addPeriodicTimeObserverForInterval:queue:usingBlock:
- (void)addPeriodicTimeObserver {
// Invoke callback every half second
CMTime interval = CMTimeMakeWithSeconds(0.5, NSEC_PER_SEC);
// Queue on which to invoke the callback
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// Add time observer
self.timeObserverToken =
[self.player addPeriodicTimeObserverForInterval:interval
queue:mainQueue
usingBlock:^(CMTime time) {
// Use weak reference to self
// Update player transport UI
}];
}
注意:添加了timeObserver后增淹,不使用的時候記得調用
- removeTimeObserver:
,否則會占用大量內存乌企。
緩沖進度:
當網絡出現(xiàn)問題時虑润,AVPlayer將會自動暫停,所以當緩沖好了之后加酵,我們必須手動調用- (void)play;
AVPlayerItem是使用KVO模式觀察狀態(tài)拳喻,所以我們可以通過觀察 loadedTimeRanges 獲取緩沖進度:
[self.player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 監(jiān)聽loadedTimeRanges屬性
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"loadedTimeRanges"]){
NSTimeInterval timeInterval = [self availableDuration];// 緩沖進度
//todo:刷新緩沖進度條、播放等邏輯
}
}
更多的功能請參考API
畫中畫的實現(xiàn)
在使用AVPlayerViewController
時猪腕,只需要簡單設置allowsPictureInPicturePlayback
冗澈,即可實現(xiàn)畫中畫功能。
那我們的自定義播放器呢陋葡?
實現(xiàn)方式我參考了https://stackoverflow.com/questions/32667090/how-to-display-avpictureinpicturecontroller
翻譯一下:
1亚亲、首先我們在Capabilities里的Background Modes設置Audio, AirPlay and Picture in Picture,如圖
2、在Appdelegate里設置
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
3捌归、在AVPlayer初始化完成后添加如下代碼
-(void)play:(NSString *)URLString {
self.player = [AVPlayer playerWithURL:[NSURL URLWithString:URLString]];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
[self.playerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
[self.layer insertSublayer:self.playerLayer below:_contentView.layer];
[_player play];
//添加畫中畫相關代碼
[self setupSuport];
}
-(void)setupSuport {
if([AVPictureInPictureController isPictureInPictureSupported]) {
//屬性變量@property(nonatomic, strong)AVPictureInPictureController *AVPictureInPictureController;
_AVPictureInPictureController = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer];
_AVPictureInPictureController.delegate = self;
} else {
// not supported PIP start button desable here
}
}
4肛响、自定義啟動畫中畫按鈕事件
if (_AVPictureInPictureController.pictureInPictureActive) {
[_AVPictureInPictureController stopPictureInPicture];
} else {
[_AVPictureInPictureController startPictureInPicture];
}
到這就完成了。
你還可以實現(xiàn)delegate惜索,以自定義更多功能特笋。
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error;
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
到這里,一個自定義播放器就基本上完成了巾兆。