記錄每天成長的點點滴滴~~~
條件,xcode7以后,ios9,對swift有一定了解
內容:多任務處理
三大特性:
1.pip---懸浮視頻播放
2.slideOver---滑動覆蓋分屏,切換任務
3.slideView---分屏,呈現兩個并行的應用
首先給大家介紹一下,pip,是三個特性中相對復雜的,全稱AVPictureInPictureController
創建流程:
步驟一:首先自定義view,需要導入,AVKit、AVFoundation庫,重寫layerClass,具體看截圖
步驟二:創建播放器(播放使用的是AVPlayer)
1.使用懶加載,創建player并添加kvo事件,監聽播放狀態
2.找到本地視頻并調用自定義的player,進行播放.
步驟三:實現pip--AVPictureInPictureController
1.調用observeValueForKeyPath,進行判斷處理
2.通過keyPath判斷,創建status,保存AVPlayerItemStatus狀態
3.最后在判斷status是否與AVPlayerItemStatus.ReadyToPlay,狀態相同
4.創建AVPictureInPictureController,并把自定義AVPlayerLayer加載到界面中,就大功告成了
請注意:iphone,目前是不支持該功能的,可以在ipad上運行.
1.如果是真機,運行程序后,點擊home鍵,退到后臺,可以看效果
2.如果是在模擬器上,點擊command+shift+h,到后臺就可以了
3.到后臺之后,視頻會繼續播放,你也可以用鼠標\手進行拖動,不過只能在四個角
效果圖附上:
模擬器中
后臺中
拖動,不同的位置
點擊視頻,可以對視頻進行處理
最后一張效果圖,個人覺得,可能是最有用的用處了,你可以邊看視頻,邊干其他的事情,圖中,邊播放視頻,另一邊打開了Safari
最后總結一下:
pip并不是支持所有的視頻
1.支持的有
AVKit
WebKit
AVFoundation
2.不支持的
MPMoviePlayerViewController
MPMoviePlayerController
pip的一些屬性、代理等
1.控制
public func startPictureInPicture()
public func stopPictureInPicture()
2.代理 AVPictureInPictureControllerDelegate
func pictureInPictureControllerWillStartPictureInPicture(pictureInPictureController:AVPictureInPictureController) {
print("will start")
}
func pictureInPictureControllerDidStartPictureInPicture(pictureInPictureController:AVPictureInPictureController) {
print("will stop")
}
func pictureInPictureController(pictureInPictureController:AVPictureInPictureController, failedToStartPictureInPictureWithError error:NSError) {
print("will fail")
}
3.pip屬性
public var pictureInPicturePossible: Bool { get }
public var pictureInPictureActive: Bool { get }
今天就這樣,寫的不好,大家別噴,希望大家早日成為大神!!!
附上:源碼,請點擊下載