提交 4333961b 编写于 作者: U u0u0

VideoPlayer 改进,tolua_pushcppstring(x,y) 改进。

上级 5c145330
......@@ -35,8 +35,8 @@ void ClippingRectangleNode::onBeforeVisitScissor()
float py = pos1.y;
float width = pos2.x - pos1.x;
float height = pos2.y - pos1.y;
assert(width > 0, "ClippingRectangle should not be rotated");
assert(height > 0, "ClippingRectangle should not be rotated");
assert(width > 0); //ClippingRectangle should not be rotated
assert(height > 0); //ClippingRectangle should not be rotated
// record old ClipRegion
GLView* glView = Director::getInstance()->getOpenGLView();
......
......@@ -32,6 +32,8 @@
#include "jni/JniHelper.h"
#include "base/CCDirector.h"
#include "base/CCEventListenerKeyboard.h"
#include "platform/CCFileUtils.h"
#include "ui/UIHelper.h"
//-----------------------------------------------------------------------------------------------------------
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxVideoHelper"
......@@ -170,18 +172,18 @@ using namespace cocos2d::experimental::ui;
static std::unordered_map<int, VideoPlayer*> s_allVideoPlayers;
VideoPlayer::VideoPlayer()
: _videoPlayerIndex(-1)
, _eventCallback(nullptr)
: _fullScreenDirty(false)
, _fullScreenEnabled(false)
, _fullScreenDirty(false)
, _keepAspectRatioEnabled(false)
, _videoPlayerIndex(-1)
, _eventCallback(nullptr)
{
_videoPlayerIndex = createVideoWidgetJNI();
s_allVideoPlayers[_videoPlayerIndex] = this;
#if CC_VIDEOPLAYER_DEBUG_DRAW
_debugDrawNode = DrawNode::create();
addchild(_debugDrawNode);
addChild(_debugDrawNode);
#endif
}
......@@ -193,7 +195,7 @@ VideoPlayer::~VideoPlayer()
void VideoPlayer::setFileName(const std::string& fileName)
{
_videoURL = fileName;
_videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
_videoSource = VideoPlayer::Source::FILENAME;
setVideoURLJNI(_videoPlayerIndex, (int)Source::FILENAME,_videoURL);
}
......@@ -211,21 +213,8 @@ void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags
if (flags & FLAGS_TRANSFORM_DIRTY)
{
auto directorInstance = Director::getInstance();
auto glView = directorInstance->getOpenGLView();
auto frameSize = glView->getFrameSize();
auto winSize = directorInstance->getWinSize();
auto leftBottom = convertToWorldSpace(Point::ZERO);
auto rightTop = convertToWorldSpace(Point(_contentSize.width,_contentSize.height));
auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX();
auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
setVideoRectJNI(_videoPlayerIndex,uiLeft,uiTop,
(rightTop.x - leftBottom.x) * glView->getScaleX(),
(rightTop.y - leftBottom.y) * glView->getScaleY());
auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(this);
setVideoRectJNI(_videoPlayerIndex, uiRect.origin.x, uiRect.origin.y, uiRect.size.width, uiRect.size.height);
}
#if CC_VIDEOPLAYER_DEBUG_DRAW
......@@ -271,7 +260,7 @@ void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
void VideoPlayer::drawDebugData()
{
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
......@@ -341,10 +330,25 @@ void VideoPlayer::setVisible(bool visible)
{
cocos2d::ui::Widget::setVisible(visible);
if (! _videoURL.empty())
if (!visible || isRunning())
{
setVideoVisible(_videoPlayerIndex,visible);
}
}
}
void VideoPlayer::onEnter()
{
Widget::onEnter();
if (isVisible() && !_videoURL.empty())
{
setVideoVisible(_videoPlayerIndex,true);
}
}
void VideoPlayer::onExit()
{
Widget::onExit();
setVideoVisible(_videoPlayerIndex,false);
}
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
......
......@@ -45,17 +45,16 @@ using namespace cocos2d::experimental::ui;
- (void) resume;
- (void) stop;
- (void) seekTo:(float) sec;
- (void) setVisible:(bool) visible;
- (void) setKeepRatioEnabled:(bool) enabled;
- (void) setFullScreenEnabled:(bool) enabled;
- (bool) isFullScreenEnabled;
- (void) setVisible:(BOOL) visible;
- (void) setKeepRatioEnabled:(BOOL) enabled;
- (void) setFullScreenEnabled:(BOOL) enabled;
- (BOOL) isFullScreenEnabled;
-(id) init:(void*) videoPlayer;
-(void) videoFinished:(NSNotification*) notification;
-(void) playStateChange;
+(NSString*) fullPathFromRelativePath:(NSString*) relPath;
@end
......@@ -66,7 +65,7 @@ using namespace cocos2d::experimental::ui;
int _width;
int _height;
bool _keepRatioEnabled;
VideoPlayer* _videoPlayer;
}
......@@ -77,7 +76,7 @@ using namespace cocos2d::experimental::ui;
_videoPlayer = (VideoPlayer*)videoPlayer;
_keepRatioEnabled = false;
}
return self;
}
......@@ -86,7 +85,7 @@ using namespace cocos2d::experimental::ui;
if (self.moviePlayer != nullptr) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nullptr;
......@@ -106,19 +105,19 @@ using namespace cocos2d::experimental::ui;
}
}
-(void) setFullScreenEnabled:(bool) enabled
-(void) setFullScreenEnabled:(BOOL) enabled
{
if (self.moviePlayer != nullptr) {
[self.moviePlayer setFullscreen:enabled animated:(true)];
}
}
-(bool) isFullScreenEnabled
-(BOOL) isFullScreenEnabled
{
if (self.moviePlayer != nullptr) {
return [self.moviePlayer isFullscreen];
}
return false;
}
......@@ -127,41 +126,41 @@ using namespace cocos2d::experimental::ui;
if (self.moviePlayer != nullptr) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
[self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nullptr;
}
if (videoSource == 1) {
self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@(videoUrl.c_str())]] autorelease];
self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease];
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.moviePlayer setContentURL:[NSURL URLWithString:@(videoUrl.c_str())]];
} else {
NSString *path = [UIVideoViewWrapperIos fullPathFromRelativePath:@(videoUrl.c_str())];
self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] autorelease];
self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]] autorelease];
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
}
self.moviePlayer.allowsAirPlay = false;
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.userInteractionEnabled = true;
auto clearColor = [UIColor clearColor];
self.moviePlayer.backgroundView.backgroundColor = clearColor;
self.moviePlayer.view.backgroundColor = clearColor;
for (UIView * subView in self.moviePlayer.view.subviews) {
subView.backgroundColor = clearColor;
}
if (_keepRatioEnabled) {
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
} else {
self.moviePlayer.scalingMode = MPMovieScalingModeFill;
}
auto view = cocos2d::Director::getInstance()->getOpenGLView();
auto eaglview = (CCEAGLView *) view->getEAGLView();
[eaglview addSubview:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
}
......@@ -170,6 +169,11 @@ using namespace cocos2d::experimental::ui;
{
if(_videoPlayer != nullptr)
{
//fix : ios 11.3 sends wrong notificaiton, ignore it
if ([self.moviePlayer playbackState] == MPMoviePlaybackStatePlaying)
{
return;
}
if([self.moviePlayer playbackState] != MPMoviePlaybackStateStopped)
{
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
......@@ -208,14 +212,14 @@ using namespace cocos2d::experimental::ui;
}
}
-(void) setVisible:(bool)visible
-(void) setVisible:(BOOL)visible
{
if (self.moviePlayer != NULL) {
[self.moviePlayer.view setHidden:!visible];
}
}
-(void) setKeepRatioEnabled:(bool)enabled
-(void) setKeepRatioEnabled:(BOOL)enabled
{
_keepRatioEnabled = enabled;
if (self.moviePlayer != NULL) {
......@@ -259,20 +263,16 @@ using namespace cocos2d::experimental::ui;
}
}
+(NSString*) fullPathFromRelativePath:(NSString*) relPath
{
return [NSString stringWithCString: cocos2d::FileUtils::getInstance()->fullPathForFilename(std::string([relPath UTF8String])).c_str() encoding: [NSString defaultCStringEncoding]];
}
@end
//------------------------------------------------------------------------------------------------------------
VideoPlayer::VideoPlayer()
: _videoPlayerIndex(-1)
, _eventCallback(nullptr)
, _fullScreenEnabled(false)
: _isPlaying(false)
, _fullScreenDirty(false)
, _fullScreenEnabled(false)
, _keepAspectRatioEnabled(false)
, _isPlaying(false)
, _videoPlayerIndex(-1)
, _eventCallback(nullptr)
{
_videoView = [[UIVideoViewWrapperIos alloc] init:this];
......@@ -292,7 +292,7 @@ VideoPlayer::~VideoPlayer()
void VideoPlayer::setFileName(const std::string& fileName)
{
_videoURL = fileName;
_videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
_videoSource = VideoPlayer::Source::FILENAME;
[((UIVideoViewWrapperIos*)_videoView) setURL:(int)_videoSource :_videoURL];
}
......@@ -307,27 +307,27 @@ void VideoPlayer::setURL(const std::string& videoUrl)
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();
......@@ -409,13 +409,32 @@ bool VideoPlayer::isPlaying() const
void VideoPlayer::setVisible(bool visible)
{
cocos2d::ui::Widget::setVisible(visible);
if (! _videoURL.empty())
if (!visible)
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:NO];
}
else if(isRunning())
{
[((UIVideoViewWrapperIos*)_videoView) setVisible:visible];
[((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;
......@@ -428,7 +447,7 @@ void VideoPlayer::onPlayEvent(int event)
} else {
_isPlaying = false;
}
if (_eventCallback)
{
_eventCallback(this, (VideoPlayer::EventType)event);
......
......@@ -29,13 +29,29 @@
#include "ui/UIWidget.h"
/**
* @addtogroup ui
* @{
*/
NS_CC_BEGIN
namespace experimental{
namespace ui{
/**
* @class VideoPlayer
* @brief Displays a video file.
*
* @note VideoPlayer displays a video file base on system widget.
* It's mean VideoPlayer displays a video file above all graphical elements of cocos2d-x.
* @js NA
*/
class VideoPlayer : public cocos2d::ui::Widget
{
public:
/**
* Videoplayer play event type.
*/
enum class EventType
{
PLAYING = 0,
......@@ -43,38 +59,121 @@ namespace experimental{
STOPPED,
COMPLETED
};
/**
* A callback which will be called after specific VideoPlayer event happens.
*/
typedef std::function<void(Ref*,VideoPlayer::EventType)> ccVideoPlayerCallback;
/**
*Static create method for instancing a VideoPlayer.
*/
CREATE_FUNC(VideoPlayer);
//Sets local file[support assets' file on android] as a video source for VideoPlayer
/**
* Sets a file path as a video source for VideoPlayer.
*/
virtual void setFileName(const std::string& videoPath);
/**
* @brief Get the local video file name.
*
* @return The video file name.
*/
virtual const std::string& getFileName() const { return _videoURL;}
//Sets network link as a video source for VideoPlayer
/**
* Sets a URL as a video source for VideoPlayer.
*/
virtual void setURL(const std::string& _videoURL);
/**
* @brief Get the URL of remoting video source.
*
* @return A remoting URL address.
*/
virtual const std::string& getURL() const { return _videoURL;}
/**
* Starts playback.
*/
virtual void play();
virtual void pause() override;
virtual void resume() override;
/**
* Pauses playback.
*/
virtual void pause()override;
/**
* Resumes playback.
*/
virtual void resume()override;
/**
* Stops playback.
*/
virtual void stop();
/**
* Seeks to specified time position.
*
* @param sec The offset in seconds from the start to seek to.
*/
virtual void seekTo(float sec);
virtual bool isPlaying() const;
virtual void setVisible(bool visible) override;
/**
* Checks whether the VideoPlayer is playing.
*
* @return True if currently playing, false otherwise.
*/
virtual bool isPlaying() const;
/**
* Causes the video player to keep aspect ratio or no when displaying the video.
*
* @param enable Specify true to keep aspect ratio or false to scale the video until
* both dimensions fit the visible bounds of the view exactly.
*/
virtual void setKeepAspectRatioEnabled(bool enable);
/**
* Indicates whether the video player keep aspect ratio when displaying the video.
*/
virtual bool isKeepAspectRatioEnabled()const { return _keepAspectRatioEnabled;}
virtual void setFullScreenEnabled(bool enabled);
/**
* Causes the video player to enter or exit full-screen mode.
*
* @param fullscreen Specify true to enter full-screen mode or false to exit full-screen mode.
*/
virtual void setFullScreenEnabled(bool fullscreen);
/**
* Indicates whether the video player is in full-screen mode.
*
* @return True if the video player is in full-screen mode, false otherwise.
*/
virtual bool isFullScreenEnabled()const;
/**
* Register a callback to be invoked when the video state is updated.
*
* @param callback The callback that will be run.
*/
virtual void addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback);
/**
* @brief A function which will be called when video is playing.
*
* @param event @see VideoPlayer::EventType.
*/
virtual void onPlayEvent(int event);
virtual void setVisible(bool visible) override;
virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override;
virtual void onEnter() override;
virtual void onExit() override;
protected:
virtual cocos2d::ui::Widget* createCloneInstance() override;
......@@ -112,6 +211,7 @@ namespace experimental{
}
NS_CC_END
// end group
/// @}
#endif
#endif
......@@ -26,7 +26,7 @@
extern "C" {
#endif
#define tolua_pushcppstring(x,y) tolua_pushstring(x,y.c_str())
#define tolua_pushcppstring(x,y) lua_pushlstring(x, y.c_str(), y.length())
#define tolua_iscppstring tolua_isstring
#define tolua_iscppstringarray tolua_isstringarray
......
......@@ -522,7 +522,7 @@ static int tolua_cocos2dx_extra_luabinding_Native_getOpenUDID00(lua_State* tolua
{
{
string tolua_ret = (string) Native::getOpenUDID();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -585,7 +585,7 @@ static int tolua_cocos2dx_extra_luabinding_Native_getInputText00(lua_State* tolu
const char* defaultValue = ((const char*) tolua_tostring(tolua_S,4,0));
{
string tolua_ret = (string) Native::getInputText(title,message,defaultValue);
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -613,7 +613,7 @@ static int tolua_cocos2dx_extra_luabinding_Native_getDeviceName00(lua_State* tol
{
{
const string tolua_ret = (const string) Native::getDeviceName();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -739,7 +739,7 @@ static int tolua_cocos2dx_extra_luabinding_HTTPRequest_getRequestUrl00(lua_State
#endif
{
string tolua_ret = (string) self->getRequestUrl();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -979,7 +979,7 @@ static int tolua_cocos2dx_extra_luabinding_HTTPRequest_getCookieString00(lua_Sta
#endif
{
string tolua_ret = (string) self->getCookieString();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -1204,7 +1204,7 @@ static int tolua_cocos2dx_extra_luabinding_HTTPRequest_getResponseHeadersString0
#endif
{
string tolua_ret = (string) self->getResponseHeadersString();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -1236,7 +1236,7 @@ static int tolua_cocos2dx_extra_luabinding_HTTPRequest_getResponseString00(lua_S
#endif
{
string tolua_ret = (string) self->getResponseString();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......@@ -1398,7 +1398,7 @@ static int tolua_cocos2dx_extra_luabinding_HTTPRequest_getErrorMessage00(lua_Sta
#endif
{
string tolua_ret = (string) self->getErrorMessage();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
tolua_pushcppstring(tolua_S, tolua_ret);
}
}
return 1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册