Last active
April 2, 2017 20:54
-
-
Save ramamilaneh/914cf8751f409ad0046be2810ac69647 to your computer and use it in GitHub Desktop.
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
-(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