AVPlayerViewController
使用方便但不夠靈活。
搜索引擎了半天并沒有找到自定義工具欄的方法。于是老老實實自己實現。
工具欄的自定義
基于AVplayer,自定義UIView,用于Player以及工具欄的顯示
@interface WKPlayerView : UIView
@property(nonatomic, strong)AVPlayer *player;
@property(nonatomic, strong)AVPlayerLayer *playerLayer;
@property(nonatomic, strong)UIControl *contentView;//用于自定義工具欄,手勢事件等。
@end
在這里,我刪除了具體的界面布局代碼。用戶期望的自定義界面或事件都在contentView上實現即可。
實現方式:
_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;
(還有更多實現)
- (CMTime)currentTime;
等api,可以靈活的根據需求自定義實現。
在這里記錄一下觀察播放進度和緩沖進度的方法:
播放進度:- 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:
,否則會占用大量內存。
緩沖進度:
當網絡出現問題時,AVPlayer將會自動暫停,所以當緩沖好了之后,我們必須手動調用- (void)play;
AVPlayerItem是使用KVO模式觀察狀態,所以我們可以通過觀察 loadedTimeRanges 獲取緩沖進度:
[self.player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 監聽loadedTimeRanges屬性
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"loadedTimeRanges"]){
NSTimeInterval timeInterval = [self availableDuration];// 緩沖進度
//todo:刷新緩沖進度條、播放等邏輯
}
}
更多的功能請參考API
畫中畫的實現
在使用AVPlayerViewController
時,只需要簡單設置allowsPictureInPicturePlayback
,即可實現畫中畫功能。
那我們的自定義播放器呢?
實現方式我參考了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];
}
到這就完成了。
你還可以實現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;
到這里,一個自定義播放器就基本上完成了。