Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meech-ward/eea70f8f08936e06797a24ad6cb44ea3 to your computer and use it in GitHub Desktop.
Save meech-ward/eea70f8f08936e06797a24ad6cb44ea3 to your computer and use it in GitHub Desktop.
iOS AVPlayerViewController
// Get the url to your video
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"keyboard_cat" withExtension:@"mp4"];
// Create a new player view controller with a player object
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *videoController = [[AVPlayerViewController alloc] init];
videoController.player = player;
// Hide the playback controls
videoController.showsPlaybackControls = NO;
// Add the video controller as a child view controller
UIView *videoView = videoController.view;
[self addChildViewController:videoController];
[self.view addSubview:videoView];
[videoController didMoveToParentViewController:self];
// Add the video controller's view
CGFloat padding = 30;
videoView.translatesAutoresizingMaskIntoConstraints = NO;
[videoView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:padding].active = YES;
[videoView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:padding].active = YES;
[videoView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-padding].active = YES;
[videoView.heightAnchor constraintEqualToAnchor:videoView.widthAnchor].active = YES;
[player play];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment