ZFPlayerController.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // ZFPlayerController.h
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import <Foundation/Foundation.h>
  25. #import <UIKit/UIKit.h>
  26. #import "ZFPlayerMediaPlayback.h"
  27. #import "ZFOrientationObserver.h"
  28. #import "ZFPlayerMediaControl.h"
  29. #import "ZFPlayerGestureControl.h"
  30. #import "ZFPlayerNotification.h"
  31. #import "ZFFloatView.h"
  32. #import "UIScrollView+ZFPlayer.h"
  33. NS_ASSUME_NONNULL_BEGIN
  34. @interface ZFPlayerController : NSObject
  35. /// The video contrainerView in normal model.
  36. @property (nonatomic, strong) UIView *containerView;
  37. /// The currentPlayerManager must conform `ZFPlayerMediaPlayback` protocol.
  38. @property (nonatomic, strong) id<ZFPlayerMediaPlayback> currentPlayerManager;
  39. /// The custom controlView must conform `ZFPlayerMediaControl` protocol.
  40. @property (nonatomic, strong) UIView<ZFPlayerMediaControl> *controlView;
  41. /// The notification manager class.
  42. @property (nonatomic, strong, readonly) ZFPlayerNotification *notification;
  43. /// The container view type.
  44. @property (nonatomic, assign, readonly) ZFPlayerContainerType containerType;
  45. /// The player's small container view.
  46. @property (nonatomic, strong, readonly) ZFFloatView *smallFloatView;
  47. /// Whether the small window is displayed.
  48. @property (nonatomic, assign, readonly) BOOL isSmallFloatViewShow;
  49. /*!
  50. @method playerWithPlayerManager:containerView:
  51. @abstract Create an ZFPlayerController that plays a single audiovisual item.
  52. @param playerManager must conform `ZFPlayerMediaPlayback` protocol.
  53. @param containerView to see the video frames must set the contrainerView.
  54. @result An instance of ZFPlayerController.
  55. */
  56. + (instancetype)playerWithPlayerManager:(id<ZFPlayerMediaPlayback>)playerManager containerView:(UIView *)containerView;
  57. /*!
  58. @method initWithPlayerManager:containerView:
  59. @abstract Create an ZFPlayerController that plays a single audiovisual item.
  60. @param playerManager must conform `ZFPlayerMediaPlayback` protocol.
  61. @param containerView to see the video frames must set the contrainerView.
  62. @result An instance of ZFPlayerController.
  63. */
  64. - (instancetype)initWithPlayerManager:(id<ZFPlayerMediaPlayback>)playerManager containerView:(UIView *)containerView;
  65. /*!
  66. @method playerWithScrollView:playerManager:containerViewTag:
  67. @abstract Create an ZFPlayerController that plays a single audiovisual item. Use in `UITableView` or `UICollectionView`.
  68. @param scrollView is `tableView` or `collectionView`.
  69. @param playerManager must conform `ZFPlayerMediaPlayback` protocol.
  70. @param containerViewTag to see the video at scrollView must set the contrainerViewTag.
  71. @result An instance of ZFPlayerController.
  72. */
  73. + (instancetype)playerWithScrollView:(UIScrollView *)scrollView playerManager:(id<ZFPlayerMediaPlayback>)playerManager containerViewTag:(NSInteger)containerViewTag;
  74. /*!
  75. @method initWithScrollView:playerManager:containerViewTag:
  76. @abstract Create an ZFPlayerController that plays a single audiovisual item. Use in `UITableView` or `UICollectionView`.
  77. @param scrollView is `tableView` or `collectionView`.
  78. @param playerManager must conform `ZFPlayerMediaPlayback` protocol.
  79. @param containerViewTag to see the video at scrollView must set the contrainerViewTag.
  80. @result An instance of ZFPlayerController.
  81. */
  82. - (instancetype)initWithScrollView:(UIScrollView *)scrollView playerManager:(id<ZFPlayerMediaPlayback>)playerManager containerViewTag:(NSInteger)containerViewTag;
  83. /*!
  84. @method playerWithScrollView:playerManager:containerView:
  85. @abstract Create an ZFPlayerController that plays a single audiovisual item. Use in `UIScrollView`.
  86. @param playerManager must conform `ZFPlayerMediaPlayback` protocol.
  87. @param containerView to see the video at the scrollView.
  88. @result An instance of ZFPlayerController.
  89. */
  90. + (instancetype)playerWithScrollView:(UIScrollView *)scrollView playerManager:(id<ZFPlayerMediaPlayback>)playerManager containerView:(UIView *)containerView;
  91. /*!
  92. @method initWithScrollView:playerManager:containerView:
  93. @abstract Create an ZFPlayerController that plays a single audiovisual item. Use in `UIScrollView`.
  94. @param playerManager must conform `ZFPlayerMediaPlayback` protocol.
  95. @param containerView to see the video at the scrollView.
  96. @result An instance of ZFPlayerController.
  97. */
  98. - (instancetype)initWithScrollView:(UIScrollView *)scrollView playerManager:(id<ZFPlayerMediaPlayback>)playerManager containerView:(UIView *)containerView;
  99. @end
  100. @interface ZFPlayerController (ZFPlayerTimeControl)
  101. /// The player current play time.
  102. @property (nonatomic, readonly) NSTimeInterval currentTime;
  103. /// The player total time.
  104. @property (nonatomic, readonly) NSTimeInterval totalTime;
  105. /// The player buffer time.
  106. @property (nonatomic, readonly) NSTimeInterval bufferTime;
  107. /// The player progress, 0...1
  108. @property (nonatomic, readonly) float progress;
  109. /// The player bufferProgress, 0...1
  110. @property (nonatomic, readonly) float bufferProgress;
  111. /**
  112. Use this method to seek to a specified time for the current player and to be notified when the seek operation is complete.
  113. @param time seek time.
  114. @param completionHandler completion handler.
  115. */
  116. - (void)seekToTime:(NSTimeInterval)time completionHandler:(void (^ __nullable)(BOOL finished))completionHandler;
  117. @end
  118. @interface ZFPlayerController (ZFPlayerPlaybackControl)
  119. /// 0...1.0
  120. /// Only affects audio volume for the device instance and not for the player.
  121. /// You can change device volume or player volume as needed,change the player volume you can conform the `ZFPlayerMediaPlayback` protocol.
  122. @property (nonatomic) float volume;
  123. /// The device muted.
  124. /// Only affects audio muting for the device instance and not for the player.
  125. /// You can change device mute or player mute as needed,change the player mute you can conform the `ZFPlayerMediaPlayback` protocol.
  126. @property (nonatomic, getter=isMuted) BOOL muted;
  127. // 0...1.0, where 1.0 is maximum brightness. Only supported by main screen.
  128. @property (nonatomic) float brightness;
  129. /// The play asset URL.
  130. @property (nonatomic) NSURL *assetURL;
  131. /// If tableView or collectionView has only one section , use `assetURLs`.
  132. /// If tableView or collectionView has more sections , use `sectionAssetURLs`.
  133. /// Set this you can use `playTheNext` `playThePrevious` `playTheIndex:` method.
  134. @property (nonatomic, copy, nullable) NSArray <NSURL *>*assetURLs;
  135. /// The currently playing index,limited to one-dimensional arrays.
  136. @property (nonatomic) NSInteger currentPlayIndex;
  137. /// is the last asset URL in `assetURLs`.
  138. @property (nonatomic, readonly) BOOL isLastAssetURL;
  139. /// is the first asset URL in `assetURLs`.
  140. @property (nonatomic, readonly) BOOL isFirstAssetURL;
  141. /// If Yes, player will be called pause method When Received `UIApplicationWillResignActiveNotification` notification.
  142. /// default is YES.
  143. @property (nonatomic) BOOL pauseWhenAppResignActive;
  144. /// When the player is playing, it is paused by some event,not by user click to pause.
  145. /// For example, when the player is playing, application goes into the background or pushed to another viewController
  146. @property (nonatomic, getter=isPauseByEvent) BOOL pauseByEvent;
  147. /// The current player controller is disappear, not dealloc
  148. @property (nonatomic, getter=isViewControllerDisappear) BOOL viewControllerDisappear;
  149. /// You can custom the AVAudioSession,
  150. /// default is NO.
  151. @property (nonatomic, assign) BOOL customAudioSession;
  152. /// The block invoked when the player is Prepare to play.
  153. @property (nonatomic, copy, nullable) void(^playerPrepareToPlay)(id<ZFPlayerMediaPlayback> asset, NSURL *assetURL);
  154. /// The block invoked when the player is Ready to play.
  155. @property (nonatomic, copy, nullable) void(^playerReadyToPlay)(id<ZFPlayerMediaPlayback> asset, NSURL *assetURL);
  156. /// The block invoked when the player play progress changed.
  157. @property (nonatomic, copy, nullable) void(^playerPlayTimeChanged)(id<ZFPlayerMediaPlayback> asset, NSTimeInterval currentTime, NSTimeInterval duration);
  158. /// The block invoked when the player play buffer changed.
  159. @property (nonatomic, copy, nullable) void(^playerBufferTimeChanged)(id<ZFPlayerMediaPlayback> asset, NSTimeInterval bufferTime);
  160. /// The block invoked when the player playback state changed.
  161. @property (nonatomic, copy, nullable) void(^playerPlayStateChanged)(id<ZFPlayerMediaPlayback> asset, ZFPlayerPlaybackState playState);
  162. /// The block invoked when the player load state changed.
  163. @property (nonatomic, copy, nullable) void(^playerLoadStateChanged)(id<ZFPlayerMediaPlayback> asset, ZFPlayerLoadState loadState);
  164. /// The block invoked when the player play failed.
  165. @property (nonatomic, copy, nullable) void(^playerPlayFailed)(id<ZFPlayerMediaPlayback> asset, id error);
  166. /// The block invoked when the player play end.
  167. @property (nonatomic, copy, nullable) void(^playerDidToEnd)(id<ZFPlayerMediaPlayback> asset);
  168. // The block invoked when video size changed.
  169. @property (nonatomic, copy, nullable) void(^presentationSizeChanged)(id<ZFPlayerMediaPlayback> asset, CGSize size);
  170. /**
  171. Play the next url ,while the `assetURLs` is not NULL.
  172. */
  173. - (void)playTheNext;
  174. /**
  175. Play the previous url ,while the `assetURLs` is not NULL.
  176. */
  177. - (void)playThePrevious;
  178. /**
  179. Play the index of url ,while the `assetURLs` is not NULL.
  180. @param index play the index.
  181. */
  182. - (void)playTheIndex:(NSInteger)index;
  183. /**
  184. Player stop and playerView remove from super view,remove other notification.
  185. */
  186. - (void)stop;
  187. /*!
  188. @method replaceCurrentPlayerManager:
  189. @abstract Replaces the player's current playeranager with the specified player item.
  190. @param manager must conform `ZFPlayerMediaPlayback` protocol
  191. @discussion The playerManager that will become the player's current playeranager.
  192. */
  193. - (void)replaceCurrentPlayerManager:(id<ZFPlayerMediaPlayback>)manager;
  194. /**
  195. Add video to the cell.
  196. */
  197. - (void)addPlayerViewToCell;
  198. /**
  199. Add video to the container view.
  200. */
  201. - (void)addPlayerViewToContainerView:(UIView *)containerView;
  202. /**
  203. Add to the keyWindow.
  204. */
  205. - (void)addPlayerViewToKeyWindow;
  206. /**
  207. Stop the current playing video and remove the playerView.
  208. */
  209. - (void)stopCurrentPlayingView;
  210. /**
  211. stop the current playing video on cell.
  212. */
  213. - (void)stopCurrentPlayingCell;
  214. @end
  215. @interface ZFPlayerController (ZFPlayerOrientationRotation)
  216. @property (nonatomic, readonly) ZFOrientationObserver *orientationObserver;
  217. /// Whether automatic screen rotation is supported.
  218. /// iOS8.1~iOS8.3 the value is YES, other iOS version the value is NO.
  219. /// This property is used for the return value of UIViewController `shouldAutorotate` method.
  220. @property (nonatomic, readonly) BOOL shouldAutorotate;
  221. /// Whether allow the video orientation rotate.
  222. /// default is YES.
  223. @property (nonatomic) BOOL allowOrentitaionRotation;
  224. /// When ZFFullScreenMode is ZFFullScreenModeLandscape the orientation is LandscapeLeft or LandscapeRight, this value is YES.
  225. /// When ZFFullScreenMode is ZFFullScreenModePortrait, while the player fullSceen this value is YES.
  226. @property (nonatomic, readonly) BOOL isFullScreen;
  227. /// when call the `stop` method, exit the fullScreen model, default is YES.
  228. @property (nonatomic, assign) BOOL exitFullScreenWhenStop;
  229. /// Lock the screen orientation.
  230. @property (nonatomic, getter=isLockedScreen) BOOL lockedScreen;
  231. /// The statusbar hidden.
  232. @property (nonatomic, getter=isStatusBarHidden) BOOL statusBarHidden;
  233. /// Use device orientation, default NO.
  234. @property (nonatomic, assign) BOOL forceDeviceOrientation;
  235. /// The current orientation of the player.
  236. /// Default is UIInterfaceOrientationPortrait.
  237. @property (nonatomic, readonly) UIInterfaceOrientation currentOrientation;
  238. /// The block invoked When player will rotate.
  239. @property (nonatomic, copy, nullable) void(^orientationWillChange)(ZFPlayerController *player, BOOL isFullScreen);
  240. /// The block invoked when player rotated.
  241. @property (nonatomic, copy, nullable) void(^orientationDidChanged)(ZFPlayerController *player, BOOL isFullScreen);
  242. /**
  243. Add the device orientation observer.
  244. */
  245. - (void)addDeviceOrientationObserver;
  246. /**
  247. Remove the device orientation observer.
  248. */
  249. - (void)removeDeviceOrientationObserver;
  250. /**
  251. Enter the fullScreen while the ZFFullScreenMode is ZFFullScreenModeLandscape.
  252. @param orientation UIInterfaceOrientation
  253. @param animated is animated.
  254. */
  255. - (void)enterLandscapeFullScreen:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
  256. /**
  257. Enter the fullScreen while the ZFFullScreenMode is ZFFullScreenModePortrait.
  258. @param fullScreen is fullscreen.
  259. @param animated is animated.
  260. */
  261. - (void)enterPortraitFullScreen:(BOOL)fullScreen animated:(BOOL)animated;
  262. /**
  263. FullScreen mode is determined by ZFFullScreenMode.
  264. @param fullScreen is fullscreen.
  265. @param animated is animated.
  266. */
  267. - (void)enterFullScreen:(BOOL)fullScreen animated:(BOOL)animated;
  268. @end
  269. @interface ZFPlayerController (ZFPlayerViewGesture)
  270. /// An instance of ZFPlayerGestureControl.
  271. @property (nonatomic, readonly) ZFPlayerGestureControl *gestureControl;
  272. /// The gesture types that the player not support.
  273. @property (nonatomic, assign) ZFPlayerDisableGestureTypes disableGestureTypes;
  274. /// The pan gesture moving direction that the player not support.
  275. @property (nonatomic) ZFPlayerDisablePanMovingDirection disablePanMovingDirection;
  276. @end
  277. @interface ZFPlayerController (ZFPlayerScrollView)
  278. /// The scroll view is `tableView` or `collectionView`.
  279. @property (nonatomic, readonly, nullable) UIScrollView *scrollView;
  280. /// The scrollView player should auto player, default is YES.
  281. @property (nonatomic) BOOL shouldAutoPlay;
  282. /// WWAN network auto play, only support in scrollView mode when the `shouldAutoPlay` is YES, default is NO.
  283. @property (nonatomic, getter=isWWANAutoPlay) BOOL WWANAutoPlay;
  284. /// The indexPath is playing.
  285. @property (nonatomic, readonly, nullable) NSIndexPath *playingIndexPath;
  286. /// The indexPath should be play while scrolling.
  287. @property (nonatomic, readonly, nullable) NSIndexPath *shouldPlayIndexPath;
  288. /// The view tag that the player display in scrollView.
  289. @property (nonatomic, readonly) NSInteger containerViewTag;
  290. /// The current playing cell stop playing when the cell has out off the screen,defalut is YES.
  291. @property (nonatomic) BOOL stopWhileNotVisible;
  292. /**
  293. The current player scroll slides off the screen percent.
  294. the property used when the `stopWhileNotVisible` is YES, stop the current playing player.
  295. the property used when the `stopWhileNotVisible` is NO, the current playing player add to small container view.
  296. The range is 0.0~1.0, defalut is 0.5.
  297. 0.0 is the player will disappear.
  298. 1.0 is the player did disappear.
  299. */
  300. @property (nonatomic) CGFloat playerDisapperaPercent;
  301. /**
  302. The current player scroll to the screen percent to play the video.
  303. The range is 0.0~1.0, defalut is 0.0.
  304. 0.0 is the player will appear.
  305. 1.0 is the player did appear.
  306. */
  307. @property (nonatomic) CGFloat playerApperaPercent;
  308. /// If tableView or collectionView has more sections, use `sectionAssetURLs`.
  309. @property (nonatomic, copy, nullable) NSArray <NSArray <NSURL *>*>*sectionAssetURLs;
  310. /// The block invoked When the player appearing.
  311. @property (nonatomic, copy, nullable) void(^zf_playerAppearingInScrollView)(NSIndexPath *indexPath, CGFloat playerApperaPercent);
  312. /// The block invoked When the player disappearing.
  313. @property (nonatomic, copy, nullable) void(^zf_playerDisappearingInScrollView)(NSIndexPath *indexPath, CGFloat playerDisapperaPercent);
  314. /// The block invoked When the player will appeared.
  315. @property (nonatomic, copy, nullable) void(^zf_playerWillAppearInScrollView)(NSIndexPath *indexPath);
  316. /// The block invoked When the player did appeared.
  317. @property (nonatomic, copy, nullable) void(^zf_playerDidAppearInScrollView)(NSIndexPath *indexPath);
  318. /// The block invoked When the player will disappear.
  319. @property (nonatomic, copy, nullable) void(^zf_playerWillDisappearInScrollView)(NSIndexPath *indexPath);
  320. /// The block invoked When the player did disappeared.
  321. @property (nonatomic, copy, nullable) void(^zf_playerDidDisappearInScrollView)(NSIndexPath *indexPath);
  322. /// The block invoked When the player should play.
  323. @property (nonatomic, copy, nullable) void(^zf_playerShouldPlayInScrollView)(NSIndexPath *indexPath);
  324. /// The block invoked When the player did stop scroll.
  325. @property (nonatomic, copy, nullable) void(^zf_scrollViewDidEndScrollingCallback)(NSIndexPath *indexPath);
  326. /// Filter the cell that should be played when the scroll is stopped (to play when the scroll is stopped).
  327. - (void)zf_filterShouldPlayCellWhileScrolled:(void (^ __nullable)(NSIndexPath *indexPath))handler;
  328. /// Filter the cell that should be played while scrolling (you can use this to filter the highlighted cell).
  329. - (void)zf_filterShouldPlayCellWhileScrolling:(void (^ __nullable)(NSIndexPath *indexPath))handler;
  330. /**
  331. Play the indexPath of url, while the `assetURLs` or `sectionAssetURLs` is not NULL.
  332. @param indexPath Play the indexPath of url.
  333. */
  334. - (void)playTheIndexPath:(NSIndexPath *)indexPath;
  335. /**
  336. Play the indexPath of url ,while the `assetURLs` or `sectionAssetURLs` is not NULL.
  337. @param indexPath Play the indexPath of url
  338. @param scrollToTop Scroll the current cell to top with animations.
  339. */
  340. - (void)playTheIndexPath:(NSIndexPath *)indexPath scrollToTop:(BOOL)scrollToTop;
  341. /**
  342. Play the indexPath of url ,while the `assetURLs` or `sectionAssetURLs` is not NULL.
  343. @param indexPath Play the indexPath of url
  344. @param assetURL The player URL.
  345. @param scrollToTop Scroll the current cell to top with animations.
  346. */
  347. - (void)playTheIndexPath:(NSIndexPath *)indexPath assetURL:(NSURL *)assetURL scrollToTop:(BOOL)scrollToTop;
  348. /**
  349. Play the indexPath of url ,while the `assetURLs` or `sectionAssetURLs` is not NULL.
  350. @param indexPath Play the indexPath of url
  351. @param scrollToTop scroll the current cell to top with animations.
  352. @param completionHandler Scroll completion callback.
  353. */
  354. - (void)playTheIndexPath:(NSIndexPath *)indexPath scrollToTop:(BOOL)scrollToTop completionHandler:(void (^ __nullable)(void))completionHandler;
  355. @end
  356. @interface ZFPlayerController (ZFPlayerDeprecated)
  357. /**
  358. Add the playerView to cell.
  359. */
  360. - (void)updateScrollViewPlayerToCell __attribute__((deprecated("use `addPlayerViewToCell:` instead.")));
  361. /**
  362. Add the playerView to containerView.
  363. @param containerView The playerView containerView.
  364. */
  365. - (void)updateNoramlPlayerWithContainerView:(UIView *)containerView __attribute__((deprecated("use `addPlayerViewToContainerView:` instead.")));
  366. @end
  367. NS_ASSUME_NONNULL_END