ASVideoNode.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // ASVideoNode.h
  3. // AsyncDisplayKit
  4. //
  5. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  6. // This source code is licensed under the BSD-style license found in the
  7. // LICENSE file in the root directory of this source tree. An additional grant
  8. // of patent rights can be found in the PATENTS file in the same directory.
  9. //
  10. #if TARGET_OS_IOS
  11. #import <AsyncDisplayKit/ASButtonNode.h>
  12. #import <AsyncDisplayKit/ASNetworkImageNode.h>
  13. @class AVAsset, AVPlayer, AVPlayerLayer, AVPlayerItem, AVVideoComposition, AVAudioMix;
  14. @protocol ASVideoNodeDelegate;
  15. typedef NS_ENUM(NSInteger, ASVideoNodePlayerState) {
  16. ASVideoNodePlayerStateUnknown,
  17. ASVideoNodePlayerStateInitialLoading,
  18. ASVideoNodePlayerStateReadyToPlay,
  19. ASVideoNodePlayerStatePlaybackLikelyToKeepUpButNotPlaying,
  20. ASVideoNodePlayerStatePlaying,
  21. ASVideoNodePlayerStateLoading,
  22. ASVideoNodePlayerStatePaused,
  23. ASVideoNodePlayerStateFinished
  24. };
  25. NS_ASSUME_NONNULL_BEGIN
  26. // IMPORTANT NOTES:
  27. // 1. Applications using ASVideoNode must link AVFoundation! (this provides the AV* classes below)
  28. // 2. This is a relatively new component of AsyncDisplayKit. It has many useful features, but
  29. // there is room for further expansion and optimization. Please report any issues or requests
  30. // in an issue on GitHub: https://github.com/facebook/AsyncDisplayKit/issues
  31. @interface ASVideoNode : ASNetworkImageNode
  32. - (void)play;
  33. - (void)pause;
  34. - (BOOL)isPlaying;
  35. - (void)resetToPlaceholder;
  36. @property (nullable, nonatomic, strong, readwrite) AVAsset *asset;
  37. /**
  38. ** @abstract The URL with which the asset was initialized.
  39. ** @discussion Setting the URL will override the current asset with a newly created AVURLAsset created from the given URL, and AVAsset *asset will point to that newly created AVURLAsset. Please don't set both assetURL and asset.
  40. ** @return Current URL the asset was initialized or nil if no URL was given.
  41. **/
  42. @property (nullable, nonatomic, strong, readwrite) NSURL *assetURL;
  43. @property (nullable, nonatomic, strong, readwrite) AVVideoComposition *videoComposition;
  44. @property (nullable, nonatomic, strong, readwrite) AVAudioMix *audioMix;
  45. @property (nullable, nonatomic, strong, readonly) AVPlayer *player;
  46. @property (nullable, nonatomic, strong, readonly) AVPlayerLayer *playerLayer;
  47. @property (nullable, nonatomic, strong, readonly) AVPlayerItem *currentItem;
  48. /**
  49. * When shouldAutoplay is set to true, a video node will play when it has both loaded and entered the "visible" interfaceState.
  50. * If it leaves the visible interfaceState it will pause but will resume once it has returned.
  51. */
  52. @property (nonatomic, assign, readwrite) BOOL shouldAutoplay;
  53. @property (nonatomic, assign, readwrite) BOOL shouldAutorepeat;
  54. @property (nonatomic, assign, readwrite) BOOL muted;
  55. @property (nonatomic, assign, readwrite) BOOL shouldAggressivelyRecoverFromStall;
  56. @property (nonatomic, assign, readonly) ASVideoNodePlayerState playerState;
  57. //! Defaults to 1000
  58. @property (nonatomic, assign) int32_t periodicTimeObserverTimescale;
  59. //! Defaults to AVLayerVideoGravityResizeAspect
  60. @property (nonatomic, copy) NSString *gravity;
  61. @property (nullable, nonatomic, weak, readwrite) id<ASVideoNodeDelegate, ASNetworkImageNodeDelegate> delegate;
  62. @end
  63. @protocol ASVideoNodeDelegate <ASNetworkImageNodeDelegate>
  64. @optional
  65. /**
  66. * @abstract Delegate method invoked when the node's video has played to its end time.
  67. * @param videoNode The video node has played to its end time.
  68. */
  69. - (void)videoDidPlayToEnd:(ASVideoNode *)videoNode;
  70. /**
  71. * @abstract Delegate method invoked the node is tapped.
  72. * @param videoNode The video node that was tapped.
  73. * @discussion The video's play state is toggled if this method is not implemented.
  74. */
  75. - (void)didTapVideoNode:(ASVideoNode *)videoNode;
  76. /**
  77. * @abstract Delegate method invoked when player changes state.
  78. * @param videoNode The video node.
  79. * @param state player state before this change.
  80. * @param toState player new state.
  81. * @discussion This method is called after each state change
  82. */
  83. - (void)videoNode:(ASVideoNode *)videoNode willChangePlayerState:(ASVideoNodePlayerState)state toState:(ASVideoNodePlayerState)toState;
  84. /**
  85. * @abstract Ssks delegate if state change is allowed
  86. * ASVideoNodePlayerStatePlaying or ASVideoNodePlayerStatePaused.
  87. * asks delegate if state change is allowed.
  88. * @param videoNode The video node.
  89. * @param state player state that is going to be set.
  90. * @discussion Delegate method invoked when player changes it's state to
  91. * ASVideoNodePlayerStatePlaying or ASVideoNodePlayerStatePaused
  92. * and asks delegate if state change is valid
  93. */
  94. - (BOOL)videoNode:(ASVideoNode*)videoNode shouldChangePlayerStateTo:(ASVideoNodePlayerState)state;
  95. /**
  96. * @abstract Delegate method invoked when player playback time is updated.
  97. * @param videoNode The video node.
  98. * @param second current playback time in seconds.
  99. */
  100. - (void)videoNode:(ASVideoNode *)videoNode didPlayToTimeInterval:(NSTimeInterval)timeInterval;
  101. /**
  102. * @abstract Delegate method invoked when the video player stalls.
  103. * @param videoNode The video node that has experienced the stall
  104. * @param second Current playback time when the stall happens
  105. */
  106. - (void)videoNode:(ASVideoNode *)videoNode didStallAtTimeInterval:(NSTimeInterval)timeInterval;
  107. /**
  108. * @abstract Delegate method invoked when the video player starts the inital asset loading
  109. * @param videoNode The videoNode
  110. */
  111. - (void)videoNodeDidStartInitialLoading:(ASVideoNode *)videoNode;
  112. /**
  113. * @abstract Delegate method invoked when the video is done loading the asset and can start the playback
  114. * @param videoNode The videoNode
  115. */
  116. - (void)videoNodeDidFinishInitialLoading:(ASVideoNode *)videoNode;
  117. /**
  118. * @abstract Delegate method invoked when the AVPlayerItem for the asset has been set up and can be accessed throught currentItem.
  119. * @param videoNode The videoNode.
  120. * @param currentItem The AVPlayerItem that was constructed from the asset.
  121. */
  122. - (void)videoNode:(ASVideoNode *)videoNode didSetCurrentItem:(AVPlayerItem *)currentItem;
  123. /**
  124. * @abstract Delegate method invoked when the video node has recovered from the stall
  125. * @param videoNode The videoNode
  126. */
  127. - (void)videoNodeDidRecoverFromStall:(ASVideoNode *)videoNode;
  128. @end
  129. @interface ASVideoNode (Unavailable)
  130. - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable;
  131. @end
  132. NS_ASSUME_NONNULL_END
  133. #endif