UIVideoPlayer-ios.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. // No Available on tvOS
  23. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS && !defined(CC_TARGET_OS_TVOS)
  24. using namespace cocos2d::experimental::ui;
  25. //-------------------------------------------------------------------------------------
  26. #include "platform/ios/CCEAGLView-ios.h"
  27. #import <MediaPlayer/MediaPlayer.h>
  28. #include "base/CCDirector.h"
  29. #include "platform/CCFileUtils.h"
  30. @interface UIVideoViewWrapperIos : NSObject
  31. @property (strong,nonatomic) MPMoviePlayerController * moviePlayer;
  32. - (void) setFrame:(int) left :(int) top :(int) width :(int) height;
  33. - (void) setURL:(int) videoSource :(std::string&) videoUrl;
  34. - (void) play;
  35. - (void) pause;
  36. - (void) resume;
  37. - (void) stop;
  38. - (void) seekTo:(float) sec;
  39. - (void) setVisible:(BOOL) visible;
  40. - (void) setKeepRatioEnabled:(BOOL) enabled;
  41. - (void) setFullScreenEnabled:(BOOL) enabled;
  42. - (BOOL) isFullScreenEnabled;
  43. -(id) init:(void*) videoPlayer;
  44. -(void) videoFinished:(NSNotification*) notification;
  45. -(void) playStateChange;
  46. @end
  47. @implementation UIVideoViewWrapperIos
  48. {
  49. int _left;
  50. int _top;
  51. int _width;
  52. int _height;
  53. bool _keepRatioEnabled;
  54. VideoPlayer* _videoPlayer;
  55. }
  56. -(id)init:(void*)videoPlayer
  57. {
  58. if (self = [super init]) {
  59. self.moviePlayer = nullptr;
  60. _videoPlayer = (VideoPlayer*)videoPlayer;
  61. _keepRatioEnabled = false;
  62. }
  63. return self;
  64. }
  65. -(void) dealloc
  66. {
  67. if (self.moviePlayer != nullptr) {
  68. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
  69. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
  70. [self.moviePlayer stop];
  71. [self.moviePlayer.view removeFromSuperview];
  72. self.moviePlayer = nullptr;
  73. _videoPlayer = nullptr;
  74. }
  75. [super dealloc];
  76. }
  77. -(void) setFrame:(int)left :(int)top :(int)width :(int)height
  78. {
  79. _left = left;
  80. _width = width;
  81. _top = top;
  82. _height = height;
  83. if (self.moviePlayer != nullptr) {
  84. [self.moviePlayer.view setFrame:CGRectMake(left, top, width, height)];
  85. }
  86. }
  87. -(void) setFullScreenEnabled:(BOOL) enabled
  88. {
  89. if (self.moviePlayer != nullptr) {
  90. [self.moviePlayer setFullscreen:enabled animated:(true)];
  91. }
  92. }
  93. -(BOOL) isFullScreenEnabled
  94. {
  95. if (self.moviePlayer != nullptr) {
  96. return [self.moviePlayer isFullscreen];
  97. }
  98. return false;
  99. }
  100. -(void) setURL:(int)videoSource :(std::string &)videoUrl
  101. {
  102. if (self.moviePlayer != nullptr) {
  103. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
  104. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
  105. [self.moviePlayer stop];
  106. [self.moviePlayer.view removeFromSuperview];
  107. self.moviePlayer = nullptr;
  108. }
  109. if (videoSource == 1) {
  110. self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease];
  111. self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
  112. [self.moviePlayer setContentURL:[NSURL URLWithString:@(videoUrl.c_str())]];
  113. } else {
  114. self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]] autorelease];
  115. self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
  116. }
  117. self.moviePlayer.allowsAirPlay = false;
  118. self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
  119. self.moviePlayer.view.userInteractionEnabled = true;
  120. auto clearColor = [UIColor clearColor];
  121. self.moviePlayer.backgroundView.backgroundColor = clearColor;
  122. self.moviePlayer.view.backgroundColor = clearColor;
  123. for (UIView * subView in self.moviePlayer.view.subviews) {
  124. subView.backgroundColor = clearColor;
  125. }
  126. if (_keepRatioEnabled) {
  127. self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
  128. } else {
  129. self.moviePlayer.scalingMode = MPMovieScalingModeFill;
  130. }
  131. auto view = cocos2d::Director::getInstance()->getOpenGLView();
  132. auto eaglview = (CCEAGLView *) view->getEAGLView();
  133. [eaglview addSubview:self.moviePlayer.view];
  134. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
  135. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
  136. }
  137. -(void) videoFinished:(NSNotification *)notification
  138. {
  139. if(_videoPlayer != nullptr)
  140. {
  141. if([self.moviePlayer playbackState] != MPMoviePlaybackStateStopped)
  142. {
  143. _videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
  144. }
  145. }
  146. }
  147. -(void) playStateChange
  148. {
  149. MPMoviePlaybackState state = [self.moviePlayer playbackState];
  150. switch (state) {
  151. case MPMoviePlaybackStatePaused:
  152. _videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
  153. break;
  154. case MPMoviePlaybackStateStopped:
  155. _videoPlayer->onPlayEvent((int)VideoPlayer::EventType::STOPPED);
  156. break;
  157. case MPMoviePlaybackStatePlaying:
  158. _videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
  159. break;
  160. case MPMoviePlaybackStateInterrupted:
  161. break;
  162. case MPMoviePlaybackStateSeekingBackward:
  163. break;
  164. case MPMoviePlaybackStateSeekingForward:
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. -(void) seekTo:(float)sec
  171. {
  172. if (self.moviePlayer != NULL) {
  173. [self.moviePlayer setCurrentPlaybackTime:(sec)];
  174. }
  175. }
  176. -(void) setVisible:(BOOL)visible
  177. {
  178. if (self.moviePlayer != NULL) {
  179. [self.moviePlayer.view setHidden:!visible];
  180. }
  181. }
  182. -(void) setKeepRatioEnabled:(BOOL)enabled
  183. {
  184. _keepRatioEnabled = enabled;
  185. if (self.moviePlayer != NULL) {
  186. if (enabled) {
  187. self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
  188. } else {
  189. self.moviePlayer.scalingMode = MPMovieScalingModeFill;
  190. }
  191. }
  192. }
  193. -(void) play
  194. {
  195. if (self.moviePlayer != NULL) {
  196. [self.moviePlayer.view setFrame:CGRectMake(_left, _top, _width, _height)];
  197. [self.moviePlayer play];
  198. }
  199. }
  200. -(void) pause
  201. {
  202. if (self.moviePlayer != NULL) {
  203. [self.moviePlayer pause];
  204. }
  205. }
  206. -(void) resume
  207. {
  208. if (self.moviePlayer != NULL) {
  209. if([self.moviePlayer playbackState] == MPMoviePlaybackStatePaused)
  210. {
  211. [self.moviePlayer play];
  212. }
  213. }
  214. }
  215. -(void) stop
  216. {
  217. if (self.moviePlayer != NULL) {
  218. [self.moviePlayer stop];
  219. }
  220. }
  221. @end
  222. //------------------------------------------------------------------------------------------------------------
  223. VideoPlayer::VideoPlayer()
  224. : _isPlaying(false)
  225. , _fullScreenDirty(false)
  226. , _fullScreenEnabled(false)
  227. , _keepAspectRatioEnabled(false)
  228. , _videoPlayerIndex(-1)
  229. , _eventCallback(nullptr)
  230. {
  231. _videoView = [[UIVideoViewWrapperIos alloc] init:this];
  232. #if CC_VIDEOPLAYER_DEBUG_DRAW
  233. _debugDrawNode = DrawNode::create();
  234. addChild(_debugDrawNode);
  235. #endif
  236. }
  237. VideoPlayer::~VideoPlayer()
  238. {
  239. if(_videoView)
  240. {
  241. [((UIVideoViewWrapperIos*)_videoView) dealloc];
  242. }
  243. }
  244. void VideoPlayer::setFileName(const std::string& fileName)
  245. {
  246. _videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
  247. _videoSource = VideoPlayer::Source::FILENAME;
  248. [((UIVideoViewWrapperIos*)_videoView) setURL:(int)_videoSource :_videoURL];
  249. }
  250. void VideoPlayer::setURL(const std::string& videoUrl)
  251. {
  252. _videoURL = videoUrl;
  253. _videoSource = VideoPlayer::Source::URL;
  254. [((UIVideoViewWrapperIos*)_videoView) setURL:(int)_videoSource :_videoURL];
  255. }
  256. void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
  257. {
  258. cocos2d::ui::Widget::draw(renderer,transform,flags);
  259. if (flags & FLAGS_TRANSFORM_DIRTY)
  260. {
  261. auto directorInstance = Director::getInstance();
  262. auto glView = directorInstance->getOpenGLView();
  263. auto frameSize = glView->getFrameSize();
  264. auto scaleFactor = [static_cast<CCEAGLView *>(glView->getEAGLView()) contentScaleFactor];
  265. auto winSize = directorInstance->getWinSize();
  266. auto leftBottom = convertToWorldSpace(Vec2::ZERO);
  267. auto rightTop = convertToWorldSpace(Vec2(_contentSize.width,_contentSize.height));
  268. auto uiLeft = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX()) / scaleFactor;
  269. auto uiTop = (frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor;
  270. [((UIVideoViewWrapperIos*)_videoView) setFrame :uiLeft :uiTop
  271. :(rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor
  272. :( (rightTop.y - leftBottom.y) * glView->getScaleY()/scaleFactor)];
  273. }
  274. #if CC_VIDEOPLAYER_DEBUG_DRAW
  275. _debugDrawNode->clear();
  276. auto size = getContentSize();
  277. Point vertices[4]=
  278. {
  279. Point::ZERO,
  280. Point(size.width, 0),
  281. Point(size.width, size.height),
  282. Point(0, size.height)
  283. };
  284. _debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
  285. #endif
  286. }
  287. bool VideoPlayer::isFullScreenEnabled()const
  288. {
  289. return [((UIVideoViewWrapperIos*)_videoView) isFullScreenEnabled];
  290. }
  291. void VideoPlayer::setFullScreenEnabled(bool enabled)
  292. {
  293. [((UIVideoViewWrapperIos*)_videoView) setFullScreenEnabled:enabled];
  294. }
  295. void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
  296. {
  297. if (_keepAspectRatioEnabled != enable)
  298. {
  299. _keepAspectRatioEnabled = enable;
  300. [((UIVideoViewWrapperIos*)_videoView) setKeepRatioEnabled:enable];
  301. }
  302. }
  303. void VideoPlayer::play()
  304. {
  305. if (! _videoURL.empty())
  306. {
  307. [((UIVideoViewWrapperIos*)_videoView) play];
  308. }
  309. }
  310. void VideoPlayer::pause()
  311. {
  312. if (! _videoURL.empty())
  313. {
  314. [((UIVideoViewWrapperIos*)_videoView) pause];
  315. }
  316. }
  317. void VideoPlayer::resume()
  318. {
  319. if (! _videoURL.empty())
  320. {
  321. [((UIVideoViewWrapperIos*)_videoView) resume];
  322. }
  323. }
  324. void VideoPlayer::stop()
  325. {
  326. if (! _videoURL.empty())
  327. {
  328. [((UIVideoViewWrapperIos*)_videoView) stop];
  329. }
  330. }
  331. void VideoPlayer::seekTo(float sec)
  332. {
  333. if (! _videoURL.empty())
  334. {
  335. [((UIVideoViewWrapperIos*)_videoView) seekTo:sec];
  336. }
  337. }
  338. bool VideoPlayer::isPlaying() const
  339. {
  340. return _isPlaying;
  341. }
  342. void VideoPlayer::setVisible(bool visible)
  343. {
  344. cocos2d::ui::Widget::setVisible(visible);
  345. if (!visible)
  346. {
  347. [((UIVideoViewWrapperIos*)_videoView) setVisible:NO];
  348. }
  349. else if(isRunning())
  350. {
  351. [((UIVideoViewWrapperIos*)_videoView) setVisible:YES];
  352. }
  353. }
  354. void VideoPlayer::onEnter()
  355. {
  356. Widget::onEnter();
  357. if (isVisible())
  358. {
  359. [((UIVideoViewWrapperIos*)_videoView) setVisible: YES];
  360. }
  361. }
  362. void VideoPlayer::onExit()
  363. {
  364. Widget::onExit();
  365. [((UIVideoViewWrapperIos*)_videoView) setVisible: NO];
  366. }
  367. void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
  368. {
  369. _eventCallback = callback;
  370. }
  371. void VideoPlayer::onPlayEvent(int event)
  372. {
  373. if (event == (int)VideoPlayer::EventType::PLAYING) {
  374. _isPlaying = true;
  375. } else {
  376. _isPlaying = false;
  377. }
  378. if (_eventCallback)
  379. {
  380. _eventCallback(this, (VideoPlayer::EventType)event);
  381. }
  382. }
  383. cocos2d::ui::Widget* VideoPlayer::createCloneInstance()
  384. {
  385. return VideoPlayer::create();
  386. }
  387. void VideoPlayer::copySpecialProperties(Widget *widget)
  388. {
  389. VideoPlayer* videoPlayer = dynamic_cast<VideoPlayer*>(widget);
  390. if (videoPlayer)
  391. {
  392. _isPlaying = videoPlayer->_isPlaying;
  393. _fullScreenEnabled = videoPlayer->_fullScreenEnabled;
  394. _fullScreenDirty = videoPlayer->_fullScreenDirty;
  395. _videoURL = videoPlayer->_videoURL;
  396. _keepAspectRatioEnabled = videoPlayer->_keepAspectRatioEnabled;
  397. _videoSource = videoPlayer->_videoSource;
  398. _videoPlayerIndex = videoPlayer->_videoPlayerIndex;
  399. _eventCallback = videoPlayer->_eventCallback;
  400. _videoView = videoPlayer->_videoView;
  401. }
  402. }
  403. #endif