UIVideoPlayer-ios.mm 13 KB

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