Skip to content

Instantly share code, notes, and snippets.

@ramamilaneh
Last active April 2, 2017 20:54
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 ramamilaneh/914cf8751f409ad0046be2810ac69647 to your computer and use it in GitHub Desktop.
Save ramamilaneh/914cf8751f409ad0046be2810ac69647 to your computer and use it in GitHub Desktop.
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self) {
[self setBackgroundColor:[UIColor whiteColor]];
// Find the video file
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"drop" ofType:@"mp4"];
// Create a new URL based on the video location
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
// Create an item using the URL
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:videoURL];
//Initializing the player with the item
_player = [AVPlayer playerWithPlayerItem:playerItem];
// Initializing the layer with player
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
//Set the frame of the player equals to the frame of the view
[_playerLayer setFrame:self.frame];
// Choose to display the video to fill the screen
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// Add the video layer to the view layer
[self.layer addSublayer:_playerLayer];
// Keep tracking when the video finishes to replay it again
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinishedPlaying:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
// Avoid cutting off the user audio
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment