記錄每天成長的點點滴滴~~~
條件,xcode7以后,ios9,對swift有一定了解
內(nèi)容:多任務(wù)處理
三大特性:
1.pip---懸浮視頻播放
2.slideOver---滑動覆蓋分屏,切換任務(wù)
3.slideView---分屏三椿,呈現(xiàn)兩個并行的應(yīng)用
首先給大家介紹一下,pip,是三個特性中相對復(fù)雜的薇溃,全稱AVPictureInPictureController
創(chuàng)建流程:
步驟一:首先自定義view菌赖,需要導(dǎo)入缭乘,AVKit、AVFoundation庫,重寫layerClass,具體看截圖
步驟二:創(chuàng)建播放器(播放使用的是AVPlayer)
1.使用懶加載琉用,創(chuàng)建player并添加kvo事件堕绩,監(jiān)聽播放狀態(tài)
2.找到本地視頻并調(diào)用自定義的player,進(jìn)行播放.
步驟三:實現(xiàn)pip--AVPictureInPictureController
1.調(diào)用observeValueForKeyPath,進(jìn)行判斷處理
2.通過keyPath判斷邑时,創(chuàng)建status奴紧,保存AVPlayerItemStatus狀態(tài)
3.最后在判斷status是否與AVPlayerItemStatus.ReadyToPlay,狀態(tài)相同
4.創(chuàng)建AVPictureInPictureController晶丘,并把自定義AVPlayerLayer加載到界面中,就大功告成了
請注意:iphone,目前是不支持該功能的黍氮,可以在ipad上運(yùn)行.
1.如果是真機(jī),運(yùn)行程序后浅浮,點擊home鍵沫浆,退到后臺,可以看效果
2.如果是在模擬器上滚秩,點擊command+shift+h,到后臺就可以了
3.到后臺之后专执,視頻會繼續(xù)播放,你也可以用鼠標(biāo)\手進(jìn)行拖動郁油,不過只能在四個角
效果圖附上:
模擬器中
后臺中
拖動本股,不同的位置
點擊視頻,可以對視頻進(jìn)行處理
最后一張效果圖,個人覺得桐腌,可能是最有用的用處了拄显,你可以邊看視頻,邊干其他的事情案站,圖中躬审,邊播放視頻,另一邊打開了Safari
最后總結(jié)一下:
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 }
今天就這樣盒件,寫的不好,大家別噴舱禽,希望大家早日成為大神!!!
附上:源碼炒刁,請點擊下載
Ios9-Multitasking-多任務(wù)處理一(懸浮視頻) - 代碼庫 - CocoaChina_讓移動開發(fā)更簡單