UIVideoPlayer-ios.mm
14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ui/UIVideoPlayer.h"
// No Available on tvOS
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS && !defined(CC_TARGET_OS_TVOS)
using namespace cocos2d::ui;
//-------------------------------------------------------------------------------------
#include "platform/ios/CCEAGLView-ios.h"
#import <AVKit/AVPlayerViewController.h>
#import <CoreMedia/CMTime.h>
#include "base/CCDirector.h"
#include "platform/CCFileUtils.h"
@interface UIVideoViewWrapperIos : NSObject
typedef NS_ENUM(NSInteger, PlayerbackState) {
PlayerbackStateUnknown = 0,
PlayerbackStatePaused,
PlayerbackStopped,
PlayerbackStatePlaying,
PlayerbackStateCompleted
};
@property (assign, nonatomic) AVPlayerViewController * playerController;
- (void) setFrame:(int) left :(int) top :(int) width :(int) height;
- (void) setURL:(int) videoSource :(std::string&) videoUrl;
- (void) play;
- (void) pause;
- (void) resume;
- (void) stop;
- (void) seekTo:(float) sec;
- (void) setVisible:(BOOL) visible;
- (void) setKeepRatioEnabled:(BOOL) enabled;
- (void) setFullScreenEnabled:(BOOL) enabled;
- (BOOL) isFullScreenEnabled;
- (void) showPlaybackControls:(BOOL) value;
- (void) setRepeatEnabled:(BOOL)enabled;
- (void) setUserInteractionEnabled:(BOOL)userInteractionEnabled;
-(id) init:(void*) videoPlayer;
-(void) videoFinished:(NSNotification*) notification;
@end
@implementation UIVideoViewWrapperIos
{
int _left;
int _top;
int _width;
int _height;
BOOL _keepRatioEnabled;
BOOL _repeatEnabled;
BOOL _showPlaybackControls;
BOOL _userInteractionEnabled;
PlayerbackState _state;
VideoPlayer* _videoPlayer;
}
-(id)init:(void*)videoPlayer
{
if (self = [super init]) {
self.playerController = [AVPlayerViewController new];
[self setRepeatEnabled:FALSE];
[self showPlaybackControls:TRUE];
[self setUserInteractionEnabled:TRUE];
[self setKeepRatioEnabled:FALSE];
_videoPlayer = (VideoPlayer*)videoPlayer;
_state = PlayerbackStateUnknown;
}
return self;
}
-(void) dealloc
{
_videoPlayer = nullptr;
[self clean];
[self.playerController release];
[super dealloc];
}
-(void) clean
{
[self stop];
[self removePlayerEventListener];
[self.playerController.view removeFromSuperview];
}
-(void) setFrame:(int)left :(int)top :(int)width :(int)height
{
_left = left;
_width = width;
_top = top;
_height = height;
[self.playerController.view setFrame:CGRectMake(left, top, width, height)];
}
-(void) setFullScreenEnabled:(BOOL) enabled
{
// AVPlayerViewController doesn't provide API to enable fullscreen. But you can toggle
// fullsreen by the playback controllers.
}
-(BOOL) isFullScreenEnabled
{
return false;
}
-(void) showPlaybackControls:(BOOL)value
{
_showPlaybackControls = value;
self.playerController.showsPlaybackControls = value;
}
-(void) setRepeatEnabled:(BOOL)enabled
{
_repeatEnabled = enabled;
if (self.playerController.player) {
if (_repeatEnabled)
self.playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
else
self.playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
}
}
-(void) setUserInteractionEnabled:(BOOL)userInteractionEnabled
{
_userInteractionEnabled = userInteractionEnabled;
self.playerController.view.userInteractionEnabled = _userInteractionEnabled;
}
-(void) setURL:(int)videoSource :(std::string &)videoUrl
{
[self clean];
if (videoSource == 1)
self.playerController.player = [[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@(videoUrl.c_str())]] autorelease];
else
self.playerController.player = [[[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]] autorelease];
[self setRepeatEnabled:_repeatEnabled];
[self setKeepRatioEnabled:_keepRatioEnabled];
[self setUserInteractionEnabled:_userInteractionEnabled];
[self showPlaybackControls:_showPlaybackControls];
auto view = cocos2d::Director::getInstance()->getOpenGLView();
auto eaglview = (CCEAGLView *) view->getEAGLView();
[eaglview addSubview:self.playerController.view];
[self registerPlayerEventListener];
}
-(void) videoFinished:(NSNotification *)notification
{
if(_videoPlayer != nullptr) {
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
_state = PlayerbackStateCompleted;
if (_repeatEnabled) {
[self seekTo:0];
[self play];
}
}
}
-(void) seekTo:(float)sec
{
if (self.playerController.player)
[self.playerController.player seekToTime:CMTimeMake(sec, 1)];
}
-(void) setVisible:(BOOL)visible
{
[self.playerController.view setHidden:!visible];
}
-(void) setKeepRatioEnabled:(BOOL)enabled
{
_keepRatioEnabled = enabled;
if (_keepRatioEnabled)
self.playerController.videoGravity = AVLayerVideoGravityResizeAspect;
else
self.playerController.videoGravity = AVLayerVideoGravityResizeAspectFill;
}
-(void) play
{
if (self.playerController.player && _state != PlayerbackStatePlaying) {
[self.playerController.player play];
_state = PlayerbackStatePlaying;
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
}
}
-(void) pause
{
if (self.playerController.player && _state == PlayerbackStatePlaying) {
[self.playerController.player pause];
_state = PlayerbackStatePaused;
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
}
}
-(void) resume
{
if (self.playerController.player && _state == PlayerbackStatePaused)
[self play];
}
-(void) stop
{
// AVPlayer doesn't have stop, so just pause it, and seek time to 0.
if (self.playerController.player && _state != PlayerbackStopped) {
[self seekTo:0];
[self.playerController.player pause];
_state = PlayerbackStopped;
// stop() will be invoked in dealloc, which is invoked by _videoPlayer's destructor,
// so do't send the message when _videoPlayer is being deleted.
if (_videoPlayer)
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::STOPPED);
}
}
-(void) registerPlayerEventListener
{
if (self.playerController.player)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinished:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerController.player.currentItem];
}
-(void) removePlayerEventListener
{
if (self.playerController.player)
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerController.player.currentItem];
}
@end
//------------------------------------------------------------------------------------------------------------
VideoPlayer::VideoPlayer()
{
_videoView = [[UIVideoViewWrapperIos alloc] init:this];
#if CC_VIDEOPLAYER_DEBUG_DRAW
_debugDrawNode = DrawNode::create();
addChild(_debugDrawNode);
#endif
}
VideoPlayer::~VideoPlayer()
{
if(_videoView)
{
[((UIVideoViewWrapperIos*)_videoView) dealloc];
}
}
void VideoPlayer::setFileName(const std::string& fileName)
{
_videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
_videoSource = VideoPlayer::Source::FILENAME;
[((UIVideoViewWrapperIos*)_videoView) setURL:(int)_videoSource :_videoURL];
}
void VideoPlayer::setURL(const std::string& videoUrl)
{
_videoURL = videoUrl;
_videoSource = VideoPlayer::Source::URL;
[((UIVideoViewWrapperIos*)_videoView) setURL:(int)_videoSource :_videoURL];
}
void VideoPlayer::setLooping(bool looping)
{
_isLooping = looping;
[((UIVideoViewWrapperIos*)_videoView) setRepeatEnabled:_isLooping];
}
void VideoPlayer::setUserInputEnabled(bool enableInput)
{
_isUserInputEnabled = enableInput;
[((UIVideoViewWrapperIos*)_videoView) setUserInteractionEnabled:enableInput];
}
void VideoPlayer::setStyle(StyleType style)
{
_styleType = style;
switch (style) {
case StyleType::DEFAULT:
[((UIVideoViewWrapperIos*)_videoView) showPlaybackControls:TRUE];
break;
case StyleType::NONE:
[((UIVideoViewWrapperIos*)_videoView) showPlaybackControls:FALSE];
break;
}
}
void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
{
cocos2d::ui::Widget::draw(renderer,transform,flags);
if (flags & FLAGS_TRANSFORM_DIRTY)
{
auto directorInstance = Director::getInstance();
auto glView = directorInstance->getOpenGLView();
auto frameSize = glView->getFrameSize();
auto scaleFactor = [static_cast<CCEAGLView *>(glView->getEAGLView()) contentScaleFactor];
auto winSize = directorInstance->getWinSize();
auto leftBottom = convertToWorldSpace(Vec2::ZERO);
auto rightTop = convertToWorldSpace(Vec2(_contentSize.width,_contentSize.height));
auto uiLeft = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX()) / scaleFactor;
auto uiTop = (frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor;
[((UIVideoViewWrapperIos*)_videoView) setFrame :uiLeft :uiTop
:(rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor
:( (rightTop.y - leftBottom.y) * glView->getScaleY()/scaleFactor)];
}
#if CC_VIDEOPLAYER_DEBUG_DRAW
_debugDrawNode->clear();
auto size = getContentSize();
Point vertices[4]=
{
Point::ZERO,
Point(size.width, 0),
Point(size.width, size.height),
Point(0, size.height)
};
_debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
#endif
}
bool VideoPlayer::isFullScreenEnabled()const
{
return [((UIVideoViewWrapperIos*)_videoView) isFullScreenEnabled];
}
void VideoPlayer::setFullScreenEnabled(bool enabled)
{
[((UIVideoViewWrapperIos*)_videoView) setFullScreenEnabled:enabled];
}
void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
{
if (_keepAspectRatioEnabled != enable)
{
_keepAspectRatioEnabled = enable;
[((UIVideoViewWrapperIos*)_videoView) setKeepRatioEnabled:enable];
}
}
void VideoPlayer::play()
{
if (! _videoURL.empty())
{
[((UIVideoViewWrapperIos*)_videoView) play];
}
}
void VideoPlayer::pause()
{
if (! _videoURL.empty())
{
[((UIVideoViewWrapperIos*)_videoView) pause];
}
}
void VideoPlayer::resume()
{
if (! _videoURL.empty())
{
[((UIVideoViewWrapperIos*)_videoView) resume];
}
}
void VideoPlayer::stop()
{
if (! _videoURL.empty())
{
[((UIVideoViewWrapperIos*)_videoView) stop];
}
}
void VideoPlayer::seekTo(float sec)
{
if (! _videoURL.empty())
{
[((UIVideoViewWrapperIos*)_videoView) seekTo:sec];
}
}
bool VideoPlayer::isPlaying() const
{
return _isPlaying;
}
bool VideoPlayer::isLooping() const
{
return _isLooping;
}
bool VideoPlayer::isUserInputEnabled() const
{
return _isUserInputEnabled;
}
void VideoPlayer::setVisible(bool visible)
{
cocos2d::ui::Widget::setVisible(visible);
if (!visible)
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:NO];
}
else if(isRunning())
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:YES];
}
}
void VideoPlayer::onEnter()
{
Widget::onEnter();
if (isVisible())
{
[((UIVideoViewWrapperIos*)_videoView) setVisible: YES];
}
}
void VideoPlayer::onExit()
{
Widget::onExit();
[((UIVideoViewWrapperIos*)_videoView) setVisible: NO];
}
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
{
_eventCallback = callback;
}
void VideoPlayer::onPlayEvent(int event)
{
if (event == (int)VideoPlayer::EventType::PLAYING) {
_isPlaying = true;
} else {
_isPlaying = false;
}
if (_eventCallback)
{
_eventCallback(this, (VideoPlayer::EventType)event);
}
}
cocos2d::ui::Widget* VideoPlayer::createCloneInstance()
{
return VideoPlayer::create();
}
void VideoPlayer::copySpecialProperties(Widget *widget)
{
VideoPlayer* videoPlayer = dynamic_cast<VideoPlayer*>(widget);
if (videoPlayer)
{
_isPlaying = videoPlayer->_isPlaying;
_isLooping = videoPlayer->_isLooping;
_isUserInputEnabled = videoPlayer->_isUserInputEnabled;
_styleType = videoPlayer->_styleType;
_fullScreenEnabled = videoPlayer->_fullScreenEnabled;
_fullScreenDirty = videoPlayer->_fullScreenDirty;
_videoURL = videoPlayer->_videoURL;
_keepAspectRatioEnabled = videoPlayer->_keepAspectRatioEnabled;
_videoSource = videoPlayer->_videoSource;
_videoPlayerIndex = videoPlayer->_videoPlayerIndex;
_eventCallback = videoPlayer->_eventCallback;
_videoView = videoPlayer->_videoView;
}
}
void VideoPlayer::setAlpha(float alpha){
((UIVideoViewWrapperIos*)_videoView).playerController.view.alpha = alpha;
}
void VideoPlayer::fadeIn(int animationTime){
[UIView animateWithDuration:animationTime animations:^{
((UIVideoViewWrapperIos*)_videoView).playerController.view.alpha = 1;
}];
}
void VideoPlayer::fadeOut(int animationTime){
[UIView animateWithDuration:animationTime animations:^{
((UIVideoViewWrapperIos*)_videoView).playerController.view.alpha = 0;
}];
}
#endif