ASVideoPlayerNode.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // ASVideoPlayerNode.h
  3. // AsyncDisplayKit
  4. //
  5. // Created by Erekle on 5/6/16.
  6. //
  7. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  8. // This source code is licensed under the BSD-style license found in the
  9. // LICENSE file in the root directory of this source tree. An additional grant
  10. // of patent rights can be found in the PATENTS file in the same directory.
  11. //
  12. #if TARGET_OS_IOS
  13. #import <AsyncDisplayKit/AsyncDisplayKit.h>
  14. //#import <AsyncDisplayKit/ASThread.h>
  15. //#import <AsyncDisplayKit/ASVideoNode.h>
  16. //#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
  17. @class AVAsset;
  18. @protocol ASVideoPlayerNodeDelegate;
  19. typedef NS_ENUM(NSInteger, ASVideoPlayerNodeControlType) {
  20. ASVideoPlayerNodeControlTypePlaybackButton,
  21. ASVideoPlayerNodeControlTypeElapsedText,
  22. ASVideoPlayerNodeControlTypeDurationText,
  23. ASVideoPlayerNodeControlTypeScrubber,
  24. ASVideoPlayerNodeControlTypeFullScreenButton,
  25. ASVideoPlayerNodeControlTypeFlexGrowSpacer,
  26. };
  27. NS_ASSUME_NONNULL_BEGIN
  28. @interface ASVideoPlayerNode : ASDisplayNode
  29. @property (nullable, nonatomic, weak) id<ASVideoPlayerNodeDelegate> delegate;
  30. @property (nonatomic, assign, readonly) CMTime duration;
  31. @property (nonatomic, assign) BOOL controlsDisabled;
  32. @property (nonatomic, assign, readonly) BOOL loadAssetWhenNodeBecomesVisible;
  33. #pragma mark - ASVideoNode property proxy
  34. /**
  35. * When shouldAutoplay is set to true, a video node will play when it has both loaded and entered the "visible" interfaceState.
  36. * If it leaves the visible interfaceState it will pause but will resume once it has returned.
  37. */
  38. @property (nonatomic, assign, readwrite) BOOL shouldAutoPlay;
  39. @property (nonatomic, assign, readwrite) BOOL shouldAutoRepeat;
  40. @property (nonatomic, assign, readwrite) BOOL muted;
  41. @property (nonatomic, assign, readonly) ASVideoNodePlayerState playerState;
  42. @property (nonatomic, assign, readwrite) BOOL shouldAggressivelyRecoverFromStall;
  43. @property (nullable, nonatomic, strong, readwrite) NSURL *placeholderImageURL;
  44. @property (nonatomic, strong, readonly) ASVideoNode *videoNode;
  45. //! Defaults to 100
  46. @property (nonatomic, assign) int32_t periodicTimeObserverTimescale;
  47. //! Defaults to AVLayerVideoGravityResizeAspect
  48. @property (nonatomic, copy) NSString *gravity;
  49. - (instancetype)initWithUrl:(NSURL*)url;
  50. - (instancetype)initWithAsset:(AVAsset*)asset;
  51. - (instancetype)initWithAsset:(AVAsset *)asset videoComposition:(AVVideoComposition *)videoComposition audioMix:(AVAudioMix *)audioMix;
  52. - (instancetype)initWithUrl:(NSURL *)url loadAssetWhenNodeBecomesVisible:(BOOL)loadAssetWhenNodeBecomesVisible;
  53. - (instancetype)initWithAsset:(AVAsset *)asset loadAssetWhenNodeBecomesVisible:(BOOL)loadAssetWhenNodeBecomesVisible;
  54. - (instancetype)initWithAsset:(AVAsset *)asset videoComposition:(AVVideoComposition *)videoComposition audioMix:(AVAudioMix *)audioMix loadAssetWhenNodeBecomesVisible:(BOOL)loadAssetWhenNodeBecomesVisible;
  55. #pragma mark - Public API
  56. - (void)seekToTime:(CGFloat)percentComplete;
  57. - (void)play;
  58. - (void)pause;
  59. - (BOOL)isPlaying;
  60. - (void)resetToPlaceholder;
  61. @end
  62. #pragma mark - ASVideoPlayerNodeDelegate -
  63. @protocol ASVideoPlayerNodeDelegate <NSObject>
  64. @optional
  65. /**
  66. * @abstract Delegate method invoked before creating controlbar controls
  67. * @param videoPlayer
  68. */
  69. - (NSArray *)videoPlayerNodeNeededDefaultControls:(ASVideoPlayerNode*)videoPlayer;
  70. /**
  71. * @abstract Delegate method invoked before creating default controls, asks delegate for custom controls dictionary.
  72. * This dictionary must constain only ASDisplayNode subclass objects.
  73. * @param videoPlayer
  74. * @discussion - This method is invoked only when developer implements videoPlayerNodeLayoutSpec:forControls:forMaximumSize:
  75. * and gives ability to add custom constrols to ASVideoPlayerNode, for example mute button.
  76. */
  77. - (NSDictionary *)videoPlayerNodeCustomControls:(ASVideoPlayerNode*)videoPlayer;
  78. /**
  79. * @abstract Delegate method invoked in layoutSpecThatFits:
  80. * @param videoPlayer
  81. * @param controls - Dictionary of controls which are used in videoPlayer; Dictionary keys are ASVideoPlayerNodeControlType
  82. * @param maxSize - Maximum size for ASVideoPlayerNode
  83. * @discussion - Developer can layout whole ASVideoPlayerNode as he wants. ASVideoNode is locked and it can't be changed
  84. */
  85. - (ASLayoutSpec *)videoPlayerNodeLayoutSpec:(ASVideoPlayerNode *)videoPlayer
  86. forControls:(NSDictionary *)controls
  87. forMaximumSize:(CGSize)maxSize;
  88. #pragma mark Text delegate methods
  89. /**
  90. * @abstract Delegate method invoked before creating ASVideoPlayerNodeControlTypeElapsedText and ASVideoPlayerNodeControlTypeDurationText
  91. * @param videoPlayer
  92. * @param timeLabelType
  93. */
  94. - (NSDictionary *)videoPlayerNodeTimeLabelAttributes:(ASVideoPlayerNode *)videoPlayerNode timeLabelType:(ASVideoPlayerNodeControlType)timeLabelType;
  95. - (NSString *)videoPlayerNode:(ASVideoPlayerNode *)videoPlayerNode
  96. timeStringForTimeLabelType:(ASVideoPlayerNodeControlType)timeLabelType
  97. forTime:(CMTime)time;
  98. #pragma mark Scrubber delegate methods
  99. - (UIColor *)videoPlayerNodeScrubberMaximumTrackTint:(ASVideoPlayerNode *)videoPlayer;
  100. - (UIColor *)videoPlayerNodeScrubberMinimumTrackTint:(ASVideoPlayerNode *)videoPlayer;
  101. - (UIColor *)videoPlayerNodeScrubberThumbTint:(ASVideoPlayerNode *)videoPlayer;
  102. - (UIImage *)videoPlayerNodeScrubberThumbImage:(ASVideoPlayerNode *)videoPlayer;
  103. #pragma mark - Spinner delegate methods
  104. - (UIColor *)videoPlayerNodeSpinnerTint:(ASVideoPlayerNode *)videoPlayer;
  105. - (UIActivityIndicatorViewStyle)videoPlayerNodeSpinnerStyle:(ASVideoPlayerNode *)videoPlayer;
  106. #pragma mark - Playback button delegate methods
  107. - (UIColor *)videoPlayerNodePlaybackButtonTint:(ASVideoPlayerNode *)videoPlayer;
  108. #pragma mark - Fullscreen button delegate methods
  109. - (UIImage *)videoPlayerNodeFullScreenButtonImage:(ASVideoPlayerNode *)videoPlayer;
  110. #pragma mark ASVideoNodeDelegate proxy methods
  111. /**
  112. * @abstract Delegate method invoked when ASVideoPlayerNode is taped.
  113. * @param videoPlayerNode The ASVideoPlayerNode that was tapped.
  114. */
  115. - (void)didTapVideoPlayerNode:(ASVideoPlayerNode *)videoPlayer;
  116. /**
  117. * @abstract Delegate method invoked when fullcreen button is taped.
  118. * @param buttonNode The fullscreen button node that was tapped.
  119. */
  120. - (void)didTapFullScreenButtonNode:(ASButtonNode *)buttonNode;
  121. /**
  122. * @abstract Delegate method invoked when ASVideoNode playback time is updated.
  123. * @param videoPlayerNode The video player node
  124. * @param second current playback time.
  125. */
  126. - (void)videoPlayerNode:(ASVideoPlayerNode *)videoPlayer didPlayToTime:(CMTime)time;
  127. /**
  128. * @abstract Delegate method invoked when ASVideoNode changes state.
  129. * @param videoPlayerNode The ASVideoPlayerNode whose ASVideoNode is changing state.
  130. * @param state ASVideoNode state before this change.
  131. * @param toSate ASVideoNode new state.
  132. * @discussion This method is called after each state change
  133. */
  134. - (void)videoPlayerNode:(ASVideoPlayerNode *)videoPlayer willChangeVideoNodeState:(ASVideoNodePlayerState)state toVideoNodeState:(ASVideoNodePlayerState)toState;
  135. /**
  136. * @abstract Delegate method is invoked when ASVideoNode decides to change state.
  137. * @param videoPlayerNode The ASVideoPlayerNode whose ASVideoNode is changing state.
  138. * @param state ASVideoNode that is going to be set.
  139. * @discussion Delegate method invoked when player changes it's state to
  140. * ASVideoNodePlayerStatePlaying or ASVideoNodePlayerStatePaused
  141. * and asks delegate if state change is valid
  142. */
  143. - (BOOL)videoPlayerNode:(ASVideoPlayerNode*)videoPlayer shouldChangeVideoNodeStateTo:(ASVideoNodePlayerState)state;
  144. /**
  145. * @abstract Delegate method invoked when the ASVideoNode has played to its end time.
  146. * @param videoPlayer The video node has played to its end time.
  147. */
  148. - (void)videoPlayerNodeDidPlayToEnd:(ASVideoPlayerNode *)videoPlayer;
  149. /**
  150. * @abstract Delegate method invoked when the ASVideoNode has constructed its AVPlayerItem for the asset.
  151. * @param videoPlayer The video player node.
  152. * @param currentItem The AVPlayerItem that was constructed from the asset.
  153. */
  154. - (void)videoPlayerNode:(ASVideoPlayerNode *)videoPlayer didSetCurrentItem:(AVPlayerItem *)currentItem;
  155. /**
  156. * @abstract Delegate method invoked when the ASVideoNode stalls.
  157. * @param videoPlayer The video player node that has experienced the stall
  158. * @param second Current playback time when the stall happens
  159. */
  160. - (void)videoPlayerNode:(ASVideoPlayerNode *)videoPlayer didStallAtTimeInterval:(NSTimeInterval)timeInterval;
  161. /**
  162. * @abstract Delegate method invoked when the ASVideoNode starts the inital asset loading
  163. * @param videoPlayer The videoPlayer
  164. */
  165. - (void)videoPlayerNodeDidStartInitialLoading:(ASVideoPlayerNode *)videoPlayer;
  166. /**
  167. * @abstract Delegate method invoked when the ASVideoNode is done loading the asset and can start the playback
  168. * @param videoPlayer The videoPlayer
  169. */
  170. - (void)videoPlayerNodeDidFinishInitialLoading:(ASVideoPlayerNode *)videoPlayer;
  171. /**
  172. * @abstract Delegate method invoked when the ASVideoNode has recovered from the stall
  173. * @param videoPlayer The videoplayer
  174. */
  175. - (void)videoPlayerNodeDidRecoverFromStall:(ASVideoPlayerNode *)videoPlayer;
  176. @end
  177. NS_ASSUME_NONNULL_END
  178. #endif