W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
最后一個(gè)圖層類型是AVPlayerLayer
。盡管它不是Core Animation框架的一部分(AV前綴看上去像),AVPlayerLayer
是有別的框架(AVFoundation)提供的,它和Core Animation緊密地結(jié)合在一起,提供了一個(gè)CALayer
子類來(lái)顯示自定義的內(nèi)容類型。
AVPlayerLayer
是用來(lái)在iOS上播放視頻的。他是高級(jí)接口例如MPMoivePlayer
的底層實(shí)現(xiàn),提供了顯示視頻的底層控制。AVPlayerLayer
的使用相當(dāng)簡(jiǎn)單:你可以用+playerLayerWithPlayer:
方法創(chuàng)建一個(gè)已經(jīng)綁定了視頻播放器的圖層,或者你可以先創(chuàng)建一個(gè)圖層,然后用player
屬性綁定一個(gè)AVPlayer
實(shí)例。
在我們開始之前,我們需要添加AVFoundation到我們的項(xiàng)目中。然后,清單6.15創(chuàng)建了一個(gè)簡(jiǎn)單的電影播放器,圖6.16是代碼運(yùn)行結(jié)果。
清單6.15 用AVPlayerLayer
播放視頻
#import "ViewController.h"
#import
#import
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *containerView; @end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//get video URL
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"Ship" withExtension:@"mp4"];
//create player and player layer
AVPlayer *player = [AVPlayer playerWithURL:URL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
//set player layer frame and attach it to our view
playerLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:playerLayer];
//play the video
[player play];
}
@end
圖6.17 3D視角下的邊框和圓角AVPlayerLayer
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: