畫中畫 (Picture in Picture)
iOS9系統在iPad上支持多任務分屏和畫中畫視頻播放,畫中畫視頻播放就將視頻播放窗口化,然后浮動在屏幕上,此時你可以使用其他APP。但是有限制:1、iOS9 2、iPad,此功能是在iPad上看電影,home返回后無意間發現的好玩的功能。
一、準備工作
1、最好確保iPad的“設置--通用--多任務--持續視頻疊層”功能打開。
2、以下為Xcode的工程相關配置
a、設置Base SDK 為 “Latest iOS”。
b、打開選項“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;
}
二、實現方式
AVKit, AVFoundation, or WebKit 分別提供了一種實現方式。
1、簡單方便快捷的實現方式使用AVKit提供的 AVPlayerViewController,定制了UI操作頁面,比如進度條、播放暫停按鈕等。
- (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、如果你想定制播放頁面,那就使用AV Foundation提供的AVPlayerLayer(具體用法請參考官方Demo,里面有AVPlayer和AVPlayerLayer的用法)和AVKit提供的 AVPictureInPictureController組合起來實現效果。具體代碼請參考官方Swift版本和Objecttive-C版。
3、使用 WebKit 提供的 WKWebView 也可以實現。
更多信息請閱讀原文。