axmol/core/ui/UIVideoPlayer-ios.mm

525 lines
15 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
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"
2019-11-23 20:27:39 +08:00
@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;
2019-11-23 20:27:39 +08:00
@end
@implementation UIVideoViewWrapperIos {
2019-11-23 20:27:39 +08:00
int _left;
int _top;
int _width;
int _height;
BOOL _keepRatioEnabled;
BOOL _repeatEnabled;
BOOL _showPlaybackControls;
BOOL _userInteractionEnabled;
PlayerbackState _state;
VideoPlayer* _videoPlayer;
}
- (id)init:(void*)videoPlayer
2019-11-23 20:27:39 +08:00
{
if (self = [super init])
{
2019-11-23 20:27:39 +08:00
self.playerController = [AVPlayerViewController new];
[self setRepeatEnabled:FALSE];
[self showPlaybackControls:TRUE];
[self setUserInteractionEnabled:TRUE];
[self setKeepRatioEnabled:FALSE];
2019-11-23 20:27:39 +08:00
_videoPlayer = (VideoPlayer*)videoPlayer;
_state = PlayerbackStateUnknown;
2019-11-23 20:27:39 +08:00
}
return self;
}
- (void)dealloc
2019-11-23 20:27:39 +08:00
{
_videoPlayer = nullptr;
[self clean];
[self.playerController release];
[super dealloc];
}
- (void)clean
2019-11-23 20:27:39 +08:00
{
[self stop];
[self removePlayerEventListener];
[self.playerController.view removeFromSuperview];
}
- (void)setFrame:(int)left:(int)top:(int)width:(int)height
2019-11-23 20:27:39 +08:00
{
_left = left;
_width = width;
_top = top;
2019-11-23 20:27:39 +08:00
_height = height;
[self.playerController.view setFrame:CGRectMake(left, top, width, height)];
}
- (void)setFullScreenEnabled:(BOOL)enabled
2019-11-23 20:27:39 +08:00
{
// AVPlayerViewController doesn't provide API to enable fullscreen. But you can toggle
// fullsreen by the playback controllers.
}
- (BOOL)isFullScreenEnabled
2019-11-23 20:27:39 +08:00
{
return false;
}
- (void)showPlaybackControls:(BOOL)value
2019-11-23 20:27:39 +08:00
{
_showPlaybackControls = value;
2019-11-23 20:27:39 +08:00
self.playerController.showsPlaybackControls = value;
}
- (void)setRepeatEnabled:(BOOL)enabled
2019-11-23 20:27:39 +08:00
{
_repeatEnabled = enabled;
if (self.playerController.player)
{
2019-11-23 20:27:39 +08:00
if (_repeatEnabled)
self.playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
else
self.playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
}
}
- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled
2019-11-23 20:27:39 +08:00
{
_userInteractionEnabled = userInteractionEnabled;
2019-11-23 20:27:39 +08:00
self.playerController.view.userInteractionEnabled = _userInteractionEnabled;
}
- (void)setURL:(int)videoSource:(std::string&)videoUrl
2019-11-23 20:27:39 +08:00
{
[self clean];
if (videoSource == 1)
self.playerController.player =
[[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@(videoUrl.c_str())]] autorelease];
2019-11-23 20:27:39 +08:00
else
self.playerController.player =
[[[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]] autorelease];
2019-11-23 20:27:39 +08:00
[self setRepeatEnabled:_repeatEnabled];
[self setKeepRatioEnabled:_keepRatioEnabled];
[self setUserInteractionEnabled:_userInteractionEnabled];
[self showPlaybackControls:_showPlaybackControls];
auto view = cocos2d::Director::getInstance()->getOpenGLView();
auto eaglview = (CCEAGLView*)view->getEAGLView();
2019-11-23 20:27:39 +08:00
[eaglview addSubview:self.playerController.view];
[self registerPlayerEventListener];
}
- (void)videoFinished:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
if (_videoPlayer != nullptr)
{
2019-11-23 20:27:39 +08:00
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
_state = PlayerbackStateCompleted;
if (_repeatEnabled)
{
2019-11-23 20:27:39 +08:00
[self seekTo:0];
[self play];
}
}
}
- (void)seekTo:(float)sec
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player)
[self.playerController.player seekToTime:CMTimeMake(sec, 1)];
}
- (void)setVisible:(BOOL)visible
2019-11-23 20:27:39 +08:00
{
[self.playerController.view setHidden:!visible];
}
- (void)setKeepRatioEnabled:(BOOL)enabled
2019-11-23 20:27:39 +08:00
{
_keepRatioEnabled = enabled;
if (_keepRatioEnabled)
self.playerController.videoGravity = AVLayerVideoGravityResizeAspect;
else
self.playerController.videoGravity = AVLayerVideoGravityResizeAspectFill;
}
- (void)play
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player && _state != PlayerbackStatePlaying)
{
2019-11-23 20:27:39 +08:00
[self.playerController.player play];
_state = PlayerbackStatePlaying;
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
}
}
- (void)pause
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player && _state == PlayerbackStatePlaying)
{
2019-11-23 20:27:39 +08:00
[self.playerController.player pause];
_state = PlayerbackStatePaused;
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
}
}
- (void)resume
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player && _state == PlayerbackStatePaused)
[self play];
}
- (void)stop
2019-11-23 20:27:39 +08:00
{
// AVPlayer doesn't have stop, so just pause it, and seek time to 0.
if (self.playerController.player && _state != PlayerbackStopped)
{
2019-11-23 20:27:39 +08:00
[self seekTo:0];
[self.playerController.player pause];
_state = PlayerbackStopped;
2019-11-23 20:27:39 +08:00
// 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
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinished:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerController.player.currentItem];
2019-11-23 20:27:39 +08:00
}
- (void)removePlayerEventListener
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player)
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerController.player.currentItem];
2019-11-23 20:27:39 +08:00
}
@end
//------------------------------------------------------------------------------------------------------------
VideoPlayer::VideoPlayer()
{
_videoView = [[UIVideoViewWrapperIos alloc] init:this];
# if CC_VIDEOPLAYER_DEBUG_DRAW
2019-11-23 20:27:39 +08:00
_debugDrawNode = DrawNode::create();
addChild(_debugDrawNode);
# endif
2019-11-23 20:27:39 +08:00
}
VideoPlayer::~VideoPlayer()
{
if (_videoView)
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoView) dealloc];
}
}
void VideoPlayer::setFileName(std::string_view fileName)
2019-11-23 20:27:39 +08:00
{
_videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
2019-11-23 20:27:39 +08:00
_videoSource = VideoPlayer::Source::FILENAME;
[((UIVideoViewWrapperIos*)_videoView) setURL:(int) _videoSource:_videoURL];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setURL(std::string_view videoUrl)
2019-11-23 20:27:39 +08:00
{
_videoURL = videoUrl;
2019-11-23 20:27:39 +08:00
_videoSource = VideoPlayer::Source::URL;
[((UIVideoViewWrapperIos*)_videoView) setURL:(int) _videoSource:_videoURL];
2019-11-23 20:27:39 +08:00
}
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;
2019-11-23 20:27:39 +08:00
case StyleType::NONE:
[((UIVideoViewWrapperIos*)_videoView) showPlaybackControls:FALSE];
break;
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
cocos2d::ui::Widget::draw(renderer, transform, flags);
2019-11-23 20:27:39 +08:00
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];
2019-11-23 20:27:39 +08:00
auto winSize = directorInstance->getWinSize();
auto leftBottom = convertToWorldSpace(Vec2::ZERO);
auto rightTop = convertToWorldSpace(Vec2(_contentSize.width, _contentSize.height));
2019-11-23 20:27:39 +08:00
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;
2019-11-23 20:27:39 +08:00
[((UIVideoViewWrapperIos*)_videoView) setFrame:
uiLeft:uiTop:(rightTop.x - leftBottom.x) * glView->getScaleX() /
scaleFactor:((rightTop.y - leftBottom.y) * glView->getScaleY() /
scaleFactor)];
2019-11-23 20:27:39 +08:00
}
# if CC_VIDEOPLAYER_DEBUG_DRAW
2019-11-23 20:27:39 +08:00
_debugDrawNode->clear();
auto size = getContentSize();
Point vertices[4] = {Point::ZERO, Point(size.width, 0), Point(size.width, size.height), Point(0, size.height)};
2019-11-23 20:27:39 +08:00
_debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
# endif
2019-11-23 20:27:39 +08:00
}
bool VideoPlayer::isFullScreenEnabled() const
2019-11-23 20:27:39 +08:00
{
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())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoView) play];
}
}
void VideoPlayer::pause()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoView) pause];
}
}
void VideoPlayer::resume()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoView) resume];
}
}
void VideoPlayer::stop()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoView) stop];
}
}
void VideoPlayer::seekTo(float sec)
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((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())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:YES];
}
}
void VideoPlayer::onEnter()
{
Widget::onEnter();
if (isVisible())
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:YES];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::onExit()
{
Widget::onExit();
[((UIVideoViewWrapperIos*)_videoView) setVisible:NO];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
{
_eventCallback = callback;
}
void VideoPlayer::onPlayEvent(int event)
{
if (event == (int)VideoPlayer::EventType::PLAYING)
{
2019-11-23 20:27:39 +08:00
_isPlaying = true;
}
else
{
2019-11-23 20:27:39 +08:00
_isPlaying = false;
}
if (_eventCallback)
{
_eventCallback(this, (VideoPlayer::EventType)event);
}
}
cocos2d::ui::Widget* VideoPlayer::createCloneInstance()
{
return VideoPlayer::create();
}
void VideoPlayer::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
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;
2019-11-23 20:27:39 +08:00
_keepAspectRatioEnabled = videoPlayer->_keepAspectRatioEnabled;
_videoSource = videoPlayer->_videoSource;
_videoPlayerIndex = videoPlayer->_videoPlayerIndex;
_eventCallback = videoPlayer->_eventCallback;
_videoView = videoPlayer->_videoView;
2019-11-23 20:27:39 +08:00
}
}
#endif