VideoPlayer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "base/ccMacros.h"
  22. #include "base/CCRef.h"
  23. #include <functional>
  24. #include <string>
  25. #include <map>
  26. #ifndef OBJC_CLASS
  27. #ifdef __OBJC__
  28. #define OBJC_CLASS(name) @class name
  29. #else
  30. #define OBJC_CLASS(name) class name
  31. #endif
  32. #endif // OBJC_CLASS
  33. NS_CC_BEGIN
  34. /**
  35. * @class VideoPlayer
  36. * @brief Displays a video file.
  37. *
  38. * @note VideoPlayer displays a video file base on system widget.
  39. * It's mean VideoPlayer displays a video file above all graphical elements of cocos2d-x.
  40. * @js NA
  41. */
  42. class VideoPlayer : public Ref
  43. {
  44. public:
  45. /**
  46. * Videoplayer play event type.
  47. */
  48. enum class EventType
  49. {
  50. PLAYING = 0,
  51. PAUSED,
  52. STOPPED,
  53. COMPLETED,
  54. META_LOADED,
  55. CLICKED,
  56. READY_TO_PLAY
  57. };
  58. VideoPlayer();
  59. /**
  60. * A callback which will be called after specific VideoPlayer event happens.
  61. */
  62. typedef std::function<void()> ccVideoPlayerCallback;
  63. /**
  64. * Sets a URL as a video source for VideoPlayer.
  65. */
  66. virtual void setURL(const std::string &_videoURL);
  67. /**
  68. * Starts playback.
  69. */
  70. virtual void play();
  71. /**
  72. * Pauses playback.
  73. */
  74. virtual void pause();
  75. /**
  76. * Stops playback.
  77. */
  78. virtual void stop();
  79. /**
  80. * Seeks to specified time position.
  81. *
  82. * @param sec The offset in seconds from the start to seek to.
  83. */
  84. virtual void seekTo(float sec);
  85. /**
  86. * Get the current play time, measure in seconds.
  87. */
  88. float currentTime() const;
  89. float duration() const;
  90. /**
  91. * Causes the video player to keep aspect ratio or no when displaying the video.
  92. *
  93. * @param enable Specify true to keep aspect ratio or false to scale the video until
  94. * both dimensions fit the visible bounds of the view exactly.
  95. */
  96. virtual void setKeepAspectRatioEnabled(bool enable);
  97. /**
  98. * Indicates whether the video player keep aspect ratio when displaying the video.
  99. */
  100. virtual bool isKeepAspectRatioEnabled() const { return _keepAspectRatioEnabled; }
  101. /**
  102. * Causes the video player to enter or exit full-screen mode.
  103. *
  104. * @param fullscreen Specify true to enter full-screen mode or false to exit full-screen mode.
  105. */
  106. virtual void setFullScreenEnabled(bool fullscreen);
  107. /**
  108. * Register a callback to be invoked when the video state is updated.
  109. *
  110. * @param callback The callback that will be run.
  111. */
  112. virtual void addEventListener(const std::string& name, const VideoPlayer::ccVideoPlayerCallback& callback);
  113. /**
  114. * @brief A function which will be called when video is playing.
  115. *
  116. * @param event @see VideoPlayer::EventType.
  117. */
  118. virtual void onPlayEvent(int event);
  119. /**
  120. * Toggle visibility of VideoPlayer.
  121. */
  122. virtual void setVisible(bool visible);
  123. /**
  124. * Set the rect of VideoPlayer.
  125. */
  126. virtual void setFrame(float x, float y, float width, float height);
  127. protected:
  128. virtual ~VideoPlayer();
  129. protected:
  130. enum class Source
  131. {
  132. FILENAME = 0,
  133. URL
  134. };
  135. bool _isVisible;
  136. bool _fullScreenDirty;
  137. bool _fullScreenEnabled;
  138. bool _keepAspectRatioEnabled;
  139. std::string _videoURL;
  140. Source _videoSource;
  141. int _videoPlayerIndex;
  142. std::map<std::string, ccVideoPlayerCallback> _eventCallback;
  143. void *_videoView;
  144. };
  145. NS_CC_END