Skip to content

Commit

Permalink
[ASVideoPlayerNode] Added an optional full screen button (#2688)
Browse files Browse the repository at this point in the history
* full screen button for ASVideoPlayerNode

* fixed a small issue when disabling controlsDisabled is set

* styling fixed

* make videoNode public on ASVidePlayerNode

* [ASVideoPlayerNode] fixed an issue when re-enabling the controls the duration time label didn't updated correctly

* [ASVideoPlayerNode] Check if CMTime is valid before setting the labels
  • Loading branch information
MarvinNazari authored and Adlai Holler committed Dec 21, 2016
1 parent 69e6f4e commit 3506cec
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions AsyncDisplayKit/ASVideoPlayerNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef enum {
ASVideoPlayerNodeControlTypeElapsedText,
ASVideoPlayerNodeControlTypeDurationText,
ASVideoPlayerNodeControlTypeScrubber,
ASVideoPlayerNodeControlTypeFullScreenButton,
ASVideoPlayerNodeControlTypeFlexGrowSpacer,
} ASVideoPlayerNodeControlType;

Expand All @@ -50,6 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign, readonly) ASVideoNodePlayerState playerState;
@property (nonatomic, assign, readwrite) BOOL shouldAggressivelyRecoverFromStall;
@property (nullable, nonatomic, strong, readwrite) NSURL *placeholderImageURL;
@property (nonatomic, strong, readonly) ASVideoNode *videoNode;

//! Defaults to 100
@property (nonatomic, assign) int32_t periodicTimeObserverTimescale;
Expand Down Expand Up @@ -125,13 +127,24 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Playback button delegate methods
- (UIColor *)videoPlayerNodePlaybackButtonTint:(ASVideoPlayerNode *)videoPlayer;

#pragma mark - Fullscreen button delegate methods

- (UIImage *)videoPlayerNodeFullScreenButtonImage:(ASVideoPlayerNode *)videoPlayer;


#pragma mark ASVideoNodeDelegate proxy methods
/**
* @abstract Delegate method invoked when ASVideoPlayerNode is taped.
* @param videoPlayerNode The ASVideoPlayerNode that was tapped.
*/
- (void)didTapVideoPlayerNode:(ASVideoPlayerNode *)videoPlayer;

/**
* @abstract Delegate method invoked when fullcreen button is taped.
* @param buttonNode The fullscreen button node that was tapped.
*/
- (void)didTapFullScreenButtonNode:(ASButtonNode *)buttonNode;

/**
* @abstract Delegate method invoked when ASVideoNode playback time is updated.
* @param videoPlayerNode The video player node
Expand Down
44 changes: 44 additions & 0 deletions AsyncDisplayKit/ASVideoPlayerNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ @interface ASVideoPlayerNode() <ASVideoNodeDelegate>
unsigned int delegateSpinnerTintColor:1;
unsigned int delegateSpinnerStyle:1;
unsigned int delegatePlaybackButtonTint:1;
unsigned int delegateFullScreenButtonImage:1;
unsigned int delegateScrubberMaximumTrackTintColor:1;
unsigned int delegateScrubberMinimumTrackTintColor:1;
unsigned int delegateScrubberThumbTintColor:1;
Expand All @@ -38,6 +39,7 @@ @interface ASVideoPlayerNode() <ASVideoNodeDelegate>
unsigned int delegateVideoNodeShouldChangeState:1;
unsigned int delegateVideoNodePlaybackDidFinish:1;
unsigned int delegateDidTapVideoPlayerNode:1;
unsigned int delegateDidTapFullScreenButtonNode:1;
unsigned int delegateVideoPlayerNodeDidSetCurrentItem:1;
unsigned int delegateVideoPlayerNodeDidStallAtTimeInterval:1;
unsigned int delegateVideoPlayerNodeDidStartInitialLoading:1;
Expand All @@ -57,6 +59,7 @@ @interface ASVideoPlayerNode() <ASVideoNodeDelegate>
NSMutableDictionary *_cachedControls;

ASDefaultPlaybackButton *_playbackButtonNode;
ASButtonNode *_fullScreenButtonNode;
ASTextNode *_elapsedTextNode;
ASTextNode *_durationTextNode;
ASDisplayNode *_scrubberNode;
Expand Down Expand Up @@ -274,6 +277,9 @@ - (void)createControls
case ASVideoPlayerNodeControlTypeScrubber:
[self createScrubber];
break;
case ASVideoPlayerNodeControlTypeFullScreenButton:
[self createFullScreenButton];
break;
case ASVideoPlayerNodeControlTypeFlexGrowSpacer:
[self createControlFlexGrowSpacer];
break;
Expand Down Expand Up @@ -315,6 +321,7 @@ - (void)cleanCachedControls
[_cachedControls removeAllObjects];

_playbackButtonNode = nil;
_fullScreenButtonNode = nil;
_elapsedTextNode = nil;
_durationTextNode = nil;
_scrubberNode = nil;
Expand Down Expand Up @@ -343,6 +350,23 @@ - (void)createPlaybackButton
[self addSubnode:_playbackButtonNode];
}

- (void)createFullScreenButton
{
if (_fullScreenButtonNode == nil) {
_fullScreenButtonNode = [[ASButtonNode alloc] init];
_fullScreenButtonNode.style.preferredSize = CGSizeMake(16.0, 22.0);

if (_delegateFlags.delegateFullScreenButtonImage) {
[_fullScreenButtonNode setImage:[_delegate videoPlayerNodeFullScreenButtonImage:self] forState:ASControlStateNormal];
}

[_fullScreenButtonNode addTarget:self action:@selector(didTapFullScreenButton:) forControlEvents:ASControlNodeEventTouchUpInside];
[_cachedControls setObject:_fullScreenButtonNode forKey:@(ASVideoPlayerNodeControlTypeFullScreenButton)];
}

[self addSubnode:_fullScreenButtonNode];
}

- (void)createElapsedTextField
{
if (_elapsedTextNode == nil) {
Expand All @@ -366,6 +390,7 @@ - (void)createDurationTextField

[_cachedControls setObject:_durationTextNode forKey:@(ASVideoPlayerNodeControlTypeDurationText)];
}
[self updateDurationTimeLabel];
[self addSubnode:_durationTextNode];
}

Expand Down Expand Up @@ -624,6 +649,11 @@ - (void)didTapPlaybackButton:(ASControlNode*)node
[self togglePlayPause];
}

- (void)didTapFullScreenButton:(ASButtonNode*)node
{
[_delegate didTapFullScreenButtonNode:node];
}

- (void)beginSeek
{
_isSeeking = YES;
Expand Down Expand Up @@ -692,6 +722,10 @@ - (NSArray *)controlsForLayoutSpec
if (_cachedControls[ @(ASVideoPlayerNodeControlTypeDurationText) ]) {
[controls addObject:_cachedControls[ @(ASVideoPlayerNodeControlTypeDurationText) ]];
}

if (_cachedControls[ @(ASVideoPlayerNodeControlTypeFullScreenButton) ]) {
[controls addObject:_cachedControls[ @(ASVideoPlayerNodeControlTypeFullScreenButton) ]];
}

return controls;
}
Expand Down Expand Up @@ -795,7 +829,9 @@ - (void)setDelegate:(id<ASVideoPlayerNodeDelegate>)delegate
_delegateFlags.delegateVideoNodeShouldChangeState = [_delegate respondsToSelector:@selector(videoPlayerNode:shouldChangeVideoNodeStateTo:)];
_delegateFlags.delegateTimeLabelAttributedString = [_delegate respondsToSelector:@selector(videoPlayerNode:timeStringForTimeLabelType:forTime:)];
_delegateFlags.delegatePlaybackButtonTint = [_delegate respondsToSelector:@selector(videoPlayerNodePlaybackButtonTint:)];
_delegateFlags.delegateFullScreenButtonImage = [_delegate respondsToSelector:@selector(videoPlayerNodeFullScreenButtonImage:)];
_delegateFlags.delegateDidTapVideoPlayerNode = [_delegate respondsToSelector:@selector(didTapVideoPlayerNode:)];
_delegateFlags.delegateDidTapFullScreenButtonNode = [_delegate respondsToSelector:@selector(didTapFullScreenButtonNode:)];
_delegateFlags.delegateVideoPlayerNodeDidSetCurrentItem = [_delegate respondsToSelector:@selector(videoPlayerNode:didSetCurrentItem:)];
_delegateFlags.delegateVideoPlayerNodeDidStallAtTimeInterval = [_delegate respondsToSelector:@selector(videoPlayerNode:didStallAtTimeInterval:)];
_delegateFlags.delegateVideoPlayerNodeDidStartInitialLoading = [_delegate respondsToSelector:@selector(videoPlayerNodeDidStartInitialLoading:)];
Expand Down Expand Up @@ -892,6 +928,11 @@ - (NSURL*) placeholderImageURL
return _videoNode.URL;
}

- (ASVideoNode*)videoNode
{
return _videoNode;
}

- (void)setShouldAggressivelyRecoverFromStall:(BOOL)shouldAggressivelyRecoverFromStall
{
if (_shouldAggressivelyRecoverFromStall == shouldAggressivelyRecoverFromStall) {
Expand All @@ -904,6 +945,9 @@ - (void)setShouldAggressivelyRecoverFromStall:(BOOL)shouldAggressivelyRecoverFro
#pragma mark - Helpers
- (NSString *)timeStringForCMTime:(CMTime)time forTimeLabelType:(ASVideoPlayerNodeControlType)type
{
if (!CMTIME_IS_VALID(time)) {
return @"00:00";
}
if (_delegateFlags.delegateTimeLabelAttributedString) {
return [_delegate videoPlayerNode:self timeStringForTimeLabelType:type forTime:time];
}
Expand Down

0 comments on commit 3506cec

Please sign in to comment.