UIVideoPlayer-android.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UIVideoPlayer.h"
  22. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  23. #include <unordered_map>
  24. #include <stdlib.h>
  25. #include <jni.h>
  26. #include <string>
  27. #include "platform/android/jni/JniHelper.h"
  28. #include "base/CCDirector.h"
  29. #include "base/CCEventListenerKeyboard.h"
  30. #include "platform/CCFileUtils.h"
  31. #include "ui/UIHelper.h"
  32. //-----------------------------------------------------------------------------------------------------------
  33. static const std::string videoHelperClassName = "org/cocos2dx/lib/Cocos2dxVideoHelper";
  34. USING_NS_CC;
  35. static void executeVideoCallback(int index,int event);
  36. #define QUIT_FULLSCREEN 1000
  37. extern "C" {
  38. void Java_org_cocos2dx_lib_Cocos2dxVideoHelper_nativeExecuteVideoCallback(JNIEnv * env, jobject obj, jint index,jint event) {
  39. executeVideoCallback(index,event);
  40. }
  41. }
  42. int createVideoWidgetJNI()
  43. {
  44. JniMethodInfo t;
  45. int ret = -1;
  46. if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "createVideoWidget", "()I")) {
  47. ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
  48. t.env->DeleteLocalRef(t.classID);
  49. }
  50. return ret;
  51. }
  52. //-----------------------------------------------------------------------------------------------------------
  53. using namespace cocos2d::experimental::ui;
  54. static std::unordered_map<int, VideoPlayer*> s_allVideoPlayers;
  55. VideoPlayer::VideoPlayer()
  56. : _fullScreenDirty(false)
  57. , _fullScreenEnabled(false)
  58. , _keepAspectRatioEnabled(false)
  59. , _videoPlayerIndex(-1)
  60. , _eventCallback(nullptr)
  61. {
  62. _videoPlayerIndex = createVideoWidgetJNI();
  63. s_allVideoPlayers[_videoPlayerIndex] = this;
  64. #if CC_VIDEOPLAYER_DEBUG_DRAW
  65. _debugDrawNode = DrawNode::create();
  66. addChild(_debugDrawNode);
  67. #endif
  68. }
  69. VideoPlayer::~VideoPlayer()
  70. {
  71. s_allVideoPlayers.erase(_videoPlayerIndex);
  72. JniHelper::callStaticVoidMethod(videoHelperClassName, "removeVideoWidget", _videoPlayerIndex);
  73. }
  74. void VideoPlayer::setFileName(const std::string& fileName)
  75. {
  76. _videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
  77. _videoSource = VideoPlayer::Source::FILENAME;
  78. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoUrl", _videoPlayerIndex,
  79. (int)Source::FILENAME,_videoURL);
  80. }
  81. void VideoPlayer::setURL(const std::string& videoUrl)
  82. {
  83. _videoURL = videoUrl;
  84. _videoSource = VideoPlayer::Source::URL;
  85. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoUrl", _videoPlayerIndex,
  86. (int)Source::URL,_videoURL);
  87. }
  88. void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
  89. {
  90. cocos2d::ui::Widget::draw(renderer,transform,flags);
  91. if (flags & FLAGS_TRANSFORM_DIRTY)
  92. {
  93. auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(this);
  94. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoRect", _videoPlayerIndex,
  95. (int)uiRect.origin.x, (int)uiRect.origin.y,
  96. (int)uiRect.size.width, (int)uiRect.size.height);
  97. }
  98. #if CC_VIDEOPLAYER_DEBUG_DRAW
  99. _debugDrawNode->clear();
  100. auto size = getContentSize();
  101. Point vertices[4]=
  102. {
  103. Point::ZERO,
  104. Point(size.width, 0),
  105. Point(size.width, size.height),
  106. Point(0, size.height)
  107. };
  108. _debugdrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
  109. #endif
  110. }
  111. void VideoPlayer::setFullScreenEnabled(bool enabled)
  112. {
  113. if (_fullScreenEnabled != enabled)
  114. {
  115. _fullScreenEnabled = enabled;
  116. auto frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
  117. JniHelper::callStaticVoidMethod(videoHelperClassName, "setFullScreenEnabled", _videoPlayerIndex,
  118. enabled, (int)frameSize.width, (int)frameSize.height);
  119. }
  120. }
  121. bool VideoPlayer::isFullScreenEnabled()const
  122. {
  123. return _fullScreenEnabled;
  124. }
  125. void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
  126. {
  127. if (_keepAspectRatioEnabled != enable)
  128. {
  129. _keepAspectRatioEnabled = enable;
  130. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoKeepRatioEnabled", _videoPlayerIndex, enable);
  131. }
  132. }
  133. #if CC_VIDEOPLAYER_DEBUG_DRAW
  134. void VideoPlayer::drawDebugData()
  135. {
  136. Director* director = Director::getInstance();
  137. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  138. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  139. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  140. auto size = getContentSize();
  141. Point vertices[4]=
  142. {
  143. Point::ZERO,
  144. Point(size.width, 0),
  145. Point(size.width, size.height),
  146. Point(0, size.height)
  147. };
  148. DrawPrimitives::drawPoly(vertices, 4, true);
  149. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  150. }
  151. #endif
  152. void VideoPlayer::play()
  153. {
  154. if (! _videoURL.empty())
  155. {
  156. JniHelper::callStaticVoidMethod(videoHelperClassName, "startVideo", _videoPlayerIndex);
  157. }
  158. }
  159. void VideoPlayer::pause()
  160. {
  161. if (! _videoURL.empty())
  162. {
  163. JniHelper::callStaticVoidMethod(videoHelperClassName, "pauseVideo", _videoPlayerIndex);
  164. }
  165. }
  166. void VideoPlayer::resume()
  167. {
  168. if (! _videoURL.empty())
  169. {
  170. JniHelper::callStaticVoidMethod(videoHelperClassName, "resumeVideo", _videoPlayerIndex);
  171. }
  172. }
  173. void VideoPlayer::stop()
  174. {
  175. if (! _videoURL.empty())
  176. {
  177. JniHelper::callStaticVoidMethod(videoHelperClassName, "stopVideo", _videoPlayerIndex);
  178. }
  179. }
  180. void VideoPlayer::seekTo(float sec)
  181. {
  182. if (! _videoURL.empty())
  183. {
  184. JniHelper::callStaticVoidMethod(videoHelperClassName, "seekVideoTo", _videoPlayerIndex, int(sec * 1000));
  185. }
  186. }
  187. bool VideoPlayer::isPlaying() const
  188. {
  189. return _isPlaying;
  190. }
  191. void VideoPlayer::setVisible(bool visible)
  192. {
  193. cocos2d::ui::Widget::setVisible(visible);
  194. if (!visible || isRunning())
  195. {
  196. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, visible);
  197. }
  198. }
  199. void VideoPlayer::onEnter()
  200. {
  201. Widget::onEnter();
  202. if (isVisible() && !_videoURL.empty())
  203. {
  204. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, true);
  205. }
  206. }
  207. void VideoPlayer::onExit()
  208. {
  209. Widget::onExit();
  210. JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, false);
  211. }
  212. void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
  213. {
  214. _eventCallback = callback;
  215. }
  216. void VideoPlayer::onPlayEvent(int event)
  217. {
  218. if (event == QUIT_FULLSCREEN)
  219. {
  220. _fullScreenEnabled = false;
  221. }
  222. else
  223. {
  224. VideoPlayer::EventType videoEvent = (VideoPlayer::EventType)event;
  225. if (videoEvent == VideoPlayer::EventType::PLAYING) {
  226. _isPlaying = true;
  227. } else {
  228. _isPlaying = false;
  229. }
  230. if (_eventCallback)
  231. {
  232. _eventCallback(this,videoEvent);
  233. }
  234. }
  235. }
  236. cocos2d::ui::Widget* VideoPlayer::createCloneInstance()
  237. {
  238. return VideoPlayer::create();
  239. }
  240. void VideoPlayer::copySpecialProperties(Widget *widget)
  241. {
  242. VideoPlayer* videoPlayer = dynamic_cast<VideoPlayer*>(widget);
  243. if (videoPlayer)
  244. {
  245. _isPlaying = videoPlayer->_isPlaying;
  246. _fullScreenEnabled = videoPlayer->_fullScreenEnabled;
  247. _fullScreenDirty = videoPlayer->_fullScreenDirty;
  248. _videoURL = videoPlayer->_videoURL;
  249. _keepAspectRatioEnabled = videoPlayer->_keepAspectRatioEnabled;
  250. _videoSource = videoPlayer->_videoSource;
  251. _videoPlayerIndex = videoPlayer->_videoPlayerIndex;
  252. _eventCallback = videoPlayer->_eventCallback;
  253. _videoView = videoPlayer->_videoView;
  254. }
  255. }
  256. void executeVideoCallback(int index,int event)
  257. {
  258. auto it = s_allVideoPlayers.find(index);
  259. if (it != s_allVideoPlayers.end())
  260. {
  261. s_allVideoPlayers[index]->onPlayEvent(event);
  262. }
  263. }
  264. #endif