Skip to content

Ui video player new style looping input options #19297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class Cocos2dxVideoHelper {
private final static int VideoTaskRestart = 10;
private final static int VideoTaskKeepRatio = 11;
private final static int VideoTaskFullScreen = 12;
private final static int VideoTaskSetLooping = 13;
private final static int VideoTaskSetUserInputEnabled = 14;
final static int KeyEventBack = 1000;

static class VideoHandler extends Handler{
Expand Down Expand Up @@ -157,11 +159,24 @@ public void handleMessage(Message msg) {
}
break;
}
case VideoTaskSetLooping: {
Cocos2dxVideoHelper helper = mReference.get();
helper._setLooping(msg.arg1, msg.arg2 != 0);
break;
}

case VideoTaskSetUserInputEnabled: {
Cocos2dxVideoHelper helper = mReference.get();
helper._setUserInputEnabled(msg.arg1, msg.arg2 != 0);
break;
}

case KeyEventBack: {
Cocos2dxVideoHelper helper = mReference.get();
helper.onBackKeyEvent();
break;
}
}

default:
break;
}
Expand Down Expand Up @@ -257,6 +272,36 @@ private void _setVideoURL(int index, int videoSource, String videoUrl) {
}
}
}

public static void setLooping(int index, boolean looping) {
Message msg = new Message();
msg.what = VideoTaskSetLooping;
msg.arg1 = index;
msg.arg2 = looping ? 1 : 0;
mVideoHandler.sendMessage(msg);
}

private void _setLooping(int index, boolean looping) {
Cocos2dxVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
videoView.setLooping(looping);
}
}

public static void setUserInputEnabled(int index, boolean enableInput) {
Message msg = new Message();
msg.what = VideoTaskSetUserInputEnabled;
msg.arg1 = index;
msg.arg2 = enableInput ? 1 : 0;
mVideoHandler.sendMessage(msg);
}

private void _setUserInputEnabled(int index, boolean enableInput) {
Cocos2dxVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
videoView.setUserInputEnabled(enableInput);
}
}

public static void setVideoRect(int index, int left, int top, int maxWidth, int maxHeight) {
Message msg = new Message();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void initVideoView() {

@Override
public boolean onTouchEvent(MotionEvent event) {
if((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)
if( mUserInputEnabled && ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP))
{
if (isPlaying()) {
pause();
Expand All @@ -211,6 +211,8 @@ public boolean onTouchEvent(MotionEvent event) {
}

private boolean mIsAssetRouse = false;
private boolean mLooping = false;
private boolean mUserInputEnabled = true;
private String mVideoFilePath = null;
private static final String AssetResourceRoot = "assets/";

Expand Down Expand Up @@ -246,6 +248,14 @@ private void setVideoURI(Uri uri, Map<String, String> headers) {
requestLayout();
invalidate();
}

public void setLooping(boolean looping) {
mLooping = looping;
}

public void setUserInputEnabled(boolean enableInput) {
mUserInputEnabled = enableInput;
}

public void stopPlayback() {
if (mMediaPlayer != null) {
Expand Down Expand Up @@ -291,6 +301,7 @@ private void openVideo() {
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setLooping(mLooping); // CROWDSTAR
//}

mDuration = -1;
Expand Down Expand Up @@ -425,8 +436,13 @@ public void onPrepared(MediaPlayer mp) {
public void onCompletion(MediaPlayer mp) {
mCurrentState = STATE_PLAYBACK_COMPLETED;
mTargetState = STATE_PLAYBACK_COMPLETED;

release(true);

// Do not release the player if we are looping as we still need the
// the player resources to exist
if (!mLooping) {
release(true);
}

if (mOnVideoEventListener != null) {
mOnVideoEventListener.onVideoEvent(mViewTag,EVENT_COMPLETED);
}
Expand All @@ -438,7 +454,8 @@ public void onCompletion(MediaPlayer mp) {
private static final int EVENT_PAUSED = 1;
private static final int EVENT_STOPPED = 2;
private static final int EVENT_COMPLETED = 3;

private static final int EVENT_ERROR = 4;

public interface OnVideoEventListener
{
void onVideoEvent(int tag,int event);
Expand All @@ -451,6 +468,10 @@ public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;

if (mOnVideoEventListener != null) {
mOnVideoEventListener.onVideoEvent(mViewTag, EVENT_ERROR);
}

/* If an error handler has been supplied, use it and finish. */
if (mOnErrorListener != null) {
if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {
Expand Down
54 changes: 54 additions & 0 deletions cocos/ui/UIVideoPlayer-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ int createVideoWidgetJNI()
return ret;
}

void setLoopingJNI(int index, bool looping)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "setLooping", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, looping);

t.env->DeleteLocalRef(t.classID);
}
}

void setUserInputEnabledJNI(int index, bool enableInput)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "setUserInputEnabled", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, enableInput);

t.env->DeleteLocalRef(t.classID);
}
}

//-----------------------------------------------------------------------------------------------------------

using namespace cocos2d::experimental::ui;
Expand All @@ -77,6 +97,10 @@ VideoPlayer::VideoPlayer()
, _keepAspectRatioEnabled(false)
, _videoPlayerIndex(-1)
, _eventCallback(nullptr)
, _isPlaying(false)
, _isLooping(false)
, _isUserInputEnabled(true)
, _styleType(StyleType::DEFAULT)
{
_videoPlayerIndex = createVideoWidgetJNI();
s_allVideoPlayers[_videoPlayerIndex] = this;
Expand Down Expand Up @@ -109,6 +133,23 @@ void VideoPlayer::setURL(const std::string& videoUrl)
(int)Source::URL,_videoURL);
}

void VideoPlayer::setLooping(bool looping)
{
_isLooping = looping;
setLoopingJNI(_videoPlayerIndex, _isLooping);
}

void VideoPlayer::setUserInputEnabled(bool enableInput)
{
_isUserInputEnabled = enableInput;
setUserInputEnabledJNI(_videoPlayerIndex, enableInput);
}

void VideoPlayer::setStyle(StyleType style)
{
_styleType = style;
}

void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
{
cocos2d::ui::Widget::draw(renderer,transform,flags);
Expand Down Expand Up @@ -231,6 +272,16 @@ 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);
Expand Down Expand Up @@ -294,6 +345,9 @@ void VideoPlayer::copySpecialProperties(Widget *widget)
if (videoPlayer)
{
_isPlaying = videoPlayer->_isPlaying;
_isLooping = videoPlayer->_isLooping;
_isUserInputEnabled = videoPlayer->_isUserInputEnabled;
_styleType = videoPlayer->_styleType;
_fullScreenEnabled = videoPlayer->_fullScreenEnabled;
_fullScreenDirty = videoPlayer->_fullScreenDirty;
_videoURL = videoPlayer->_videoURL;
Expand Down
Loading