Skip to content

Instantly share code, notes, and snippets.

@bendiksolheim
Created January 20, 2015 15:05
Show Gist options
  • Save bendiksolheim/1f355048853ce1e6102b to your computer and use it in GitHub Desktop.
Save bendiksolheim/1f355048853ce1e6102b to your computer and use it in GitHub Desktop.
Play video in UIView
#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