畫中畫 (Picture in Picture)
iOS9系統(tǒng)在iPad上支持多任務(wù)分屏和畫中畫視頻播放悲立,畫中畫視頻播放就將視頻播放窗口化跟啤,然后浮動(dòng)在屏幕上捺球,此時(shí)你可以使用其他APP滑潘。但是有限制:1、iOS9 2查近、iPad眉踱,此功能是在iPad上看電影,home返回后無(wú)意間發(fā)現(xiàn)的好玩的功能霜威。
一谈喳、準(zhǔn)備工作
1、最好確保iPad的“設(shè)置--通用--多任務(wù)--持續(xù)視頻疊層”功能打開(kāi)戈泼。
2婿禽、以下為Xcode的工程相關(guān)配置
a、設(shè)置Base SDK 為 “Latest iOS”大猛。
b扭倾、打開(kāi)選項(xiàng)“target--Capabilities--Background Modes--Audio, AirPlay and Picture in Picture”。
c挽绩、確保APP的audio session使用正確的類型AVAudioSessionCategoryPlayback
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
return YES;
}
二膛壹、實(shí)現(xiàn)方式
AVKit, AVFoundation, or WebKit 分別提供了一種實(shí)現(xiàn)方式。
1唉堪、簡(jiǎn)單方便快捷的實(shí)現(xiàn)方式使用AVKit提供的 AVPlayerViewController模聋,定制了UI操作頁(yè)面,比如進(jìn)度條唠亚、播放暫停按鈕等链方。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
//AVKit提供的 AVPlayerViewController
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:@"samplemovie" withExtension:@"mov"];
AVURLAsset *asset =[AVURLAsset assetWithURL:movieURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer* player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerViewController *playerCtr = [[AVPlayerViewController alloc] init];
playerCtr.player = player;
[playerCtr.player play];
self.window.rootViewController = playerCtr;
return YES;
}
2、如果你想定制播放頁(yè)面灶搜,那就使用AV Foundation提供的AVPlayerLayer(具體用法請(qǐng)參考官方Demo祟蚀,里面有AVPlayer和AVPlayerLayer的用法)和AVKit提供的 AVPictureInPictureController組合起來(lái)實(shí)現(xiàn)效果。具體代碼請(qǐng)參考官方Swift版本和Objecttive-C版割卖。
3前酿、使用 WebKit 提供的 WKWebView 也可以實(shí)現(xiàn)。
更多信息請(qǐng)閱讀原文究珊。