Created
January 20, 2015 15:05
-
-
Save bendiksolheim/1f355048853ce1e6102b to your computer and use it in GitHub Desktop.
Play video in UIView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import <QuartzCore/QuartzCore.h> | |
#import <AVFoundation/AVFoundation.h> | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSBundle *bundle = [NSBundle mainBundle]; | |
NSString *moviePath = [bundle pathForResource:@"movie" ofType:@"mp4"]; | |
CGFloat width = self.view.frame.size.width; | |
CGFloat height = self.view.frame.size.height / 3.0; | |
AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]]; | |
AVPlayerLayer *playerLayer = [AVPlayerLayer layer]; | |
playerLayer.player = player; | |
playerLayer.frame = CGRectMake(.0, height, width, height); | |
playerLayer.backgroundColor = [UIColor blackColor].CGColor; | |
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; | |
[self.view.layer addSublayer:playerLayer]; | |
[player play]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment