Last active
February 23, 2016 23:41
-
-
Save MACastro987/f62043396641b16082c0 to your computer and use it in GitHub Desktop.
TableViewCell AVPlayer
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
- (void)awakeFromNib { | |
self.playButton.layer.hidden = YES; | |
_playButtonTapped = NO; | |
} | |
- (void)setFeedItem:(PFObject *)feedItem { | |
_feedItem = feedItem; | |
PFUser *user = [feedItem objectForKey:@"user"]; | |
[_usernameButton setTitle:user.username | |
forState:UIControlStateNormal]; | |
_captionView.text = [feedItem objectForKey:@"desc"]; | |
_timestampLabel.text = [NSDate cks_stringForTimeSinceDate:feedItem.createdAt]; | |
_pictureView.image = self.feedImage; | |
//_____________________________Handle Video_________________________________ | |
_isAVideo = [[feedItem objectForKey:@"isAVideo"]boolValue]; | |
if (_isAVideo) { | |
// ------------- load the video asset --------------- | |
PFQuery *query = [PFQuery queryWithClassName:@"tfeed"]; | |
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { | |
PFFile *videoFile = [feedItem objectForKey:@"video"]; | |
[videoFile getDataInBackgroundWithBlock:^(NSData * _Nullable data, NSError * _Nullable error){ | |
if (!error) { | |
// Initialize AVPlayer with video file | |
NSLog(@"videoFile URL: %@", videoFile.url); // log the URL of the videoObject | |
NSURL *videoURL = [NSURL URLWithString:videoFile.url]; | |
self.playerItem = [AVPlayerItem playerItemWithURL:videoURL]; // playerItem with object at videoPath | |
self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem]; | |
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; | |
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; | |
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; | |
self.playerLayer.frame = CGRectMake(0,0, self.cellView.frame.size.width, self.cellView.frame.size.height); | |
[self.playerLayer setFrame:self.cellView.bounds]; | |
[self.cellView.layer addSublayer:self.playerLayer]; | |
[self initializeActivityIndicator]; | |
// add the play button | |
[self.cellView addSubview:self.playButton]; | |
[self.cellView bringSubviewToFront:self.playButton]; | |
} | |
}]; | |
}]; | |
} else if (!_isAVideo) { | |
NSLog(@"This is NOT a video"); | |
} | |
if(!self.isAVideo) { | |
[self.playerLayer setHidden:YES]; | |
[self.playButton setHidden:YES]; | |
} else { | |
[self.playerLayer setHidden:NO]; | |
[self.playButton setHidden:NO]; | |
} | |
} | |
- (void)initializeObservers { | |
[_playerItem addObserver:self forKeyPath:@"status" options:0 context:nil]; | |
[_playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil]; | |
[_playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil]; | |
[_playerItem addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionNew context:nil]; | |
} | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object | |
change:(NSDictionary *)change context:(void *)context { | |
if (object == _playerItem | |
&& [keyPath isEqualToString:@"playbackBufferEmpty"]) | |
{ | |
if (_playerItem.playbackBufferEmpty) | |
{ | |
// show loading indicator | |
NSLog(@"BUFFER EMPTY"); | |
} | |
} | |
if (object == _playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"]) | |
{ | |
if (_playerItem.playbackLikelyToKeepUp) | |
{ | |
// hide loading indicator | |
[_indicatorView stopAnimating]; | |
if (_playerItem.status == AVPlayerItemStatusReadyToPlay) { | |
// start playing | |
NSLog(@"READY TO PLAY"); | |
if (_playButtonTapped){ | |
_playButtonTapped = NO; | |
[_player play]; | |
//[_playerItem removeObserver:self forKeyPath:@"status"]; | |
} | |
} | |
else if (_playerItem.status == AVPlayerStatusFailed) { | |
// handle failed | |
NSLog(@"STATUS FAILED"); | |
} | |
else if (_playerItem.status == AVPlayerStatusUnknown) { | |
// handle unknown | |
NSLog(@"STATUS UNKNOWN"); | |
} | |
} | |
} | |
} | |
- (void)initializeActivityIndicator { | |
self.indicatorView = [[MONActivityIndicatorView alloc] init]; | |
self.indicatorView.delegate = self; | |
self.indicatorView.numberOfCircles = 3; | |
self.indicatorView.radius = 20; | |
self.indicatorView.internalSpacing = 3; | |
self.indicatorView.center = self.cellView.center; | |
[self.cellView addSubview:self.indicatorView]; | |
[self.cellView addConstraint:[NSLayoutConstraint constraintWithItem:self.indicatorView attribute:NSLayoutAttributeCenterX | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.cellView | |
attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; | |
[self.cellView addConstraint:[NSLayoutConstraint constraintWithItem:self.indicatorView | |
attribute:NSLayoutAttributeCenterY | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.cellView | |
attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]]; | |
} | |
- (IBAction)playButtonTapped:(id)sender { | |
_playButtonTapped = YES; | |
_playButton.hidden = YES; | |
_playButton.enabled = NO; | |
NSLog(@"playButtonTapped"); | |
[self initializeObservers]; | |
[_indicatorView startAnimating]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector: @selector (playerItemDidReachEnd:) | |
name:AVPlayerItemDidPlayToEndTimeNotification | |
object:[self.player currentItem]]; | |
} | |
- (void)playerItemDidReachEnd:(NSNotification *)notification { | |
NSLog(@"PLAYERITEMDIDREACHEND"); | |
self.playerItem = [notification object]; | |
_playButtonTapped = NO; | |
_playButton.hidden = NO; | |
_playButton.enabled = YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment