Graphics Reference
In-Depth Information
Listing 6.15
Playing a Video with
AVPlayerLayer
#import
"ViewController.h"
#import
<QuartzCore/QuartzCore.h>
#import
<AVFoundation/AVFoundation.h>
@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