Skip to content

Commit

Permalink
add some native feature for videoplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyduckx committed Aug 24, 2023
1 parent dee666c commit 40ea3c0
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public class CocosVideoHelper {
private final static int VideoTaskKeepRatio = 11;
private final static int VideoTaskFullScreen = 12;
private final static int VideoTaskSetVolume = 13;
private final static int VideoTaskSetPlaybackRate = 14;
private final static int VideoTaskSetMute = 15;
private final static int VideoTaskSetLoop = 16;


final static int KeyEventBack = 1000;

Expand Down Expand Up @@ -156,6 +160,27 @@ public void handleMessage(Message msg) {
helper._setVolume(msg.arg1, volume);
break;
}
case VideoTaskSetPlaybackRate: {
float rate = (float) msg.arg2 / 10;
helper._setPlaybackRate(msg.arg1, rate);
break;
}
case VideoTaskSetMute: {
if (msg.arg2 == 1) {
helper._setMute(msg.arg1, true);
} else {
helper._setMute(msg.arg1, false);
}
break;
}
case VideoTaskSetLoop: {
if (msg.arg2 == 1) {
helper._setLoop(msg.arg1, true);
} else {
helper._setLoop(msg.arg1, false);
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -407,6 +432,59 @@ public static void setVideoKeepRatioEnabled(int index, boolean enable) {
mVideoHandler.sendMessage(msg);
}

public static void setPlaybackRate(final int index, final float value) {
Message msg = new Message();
msg.what = VideoTaskSetPlaybackRate;
msg.arg1 = index;
msg.arg2 = (int) (value * 10);
mVideoHandler.sendMessage(msg);
}

private void _setPlaybackRate(final int index, final float value) {
CocosVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
videoView.setPlaybackRate(value);
}
}

public static void setMute(int index, boolean enable) {
Message msg = new Message();
msg.what = VideoTaskSetMute;
msg.arg1 = index;
if (enable) {
msg.arg2 = 1;
} else {
msg.arg2 = 0;
}
mVideoHandler.sendMessage(msg);
}

private void _setMute(int index, boolean enable) {
CocosVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
videoView.setMute(enable);
}
}

public static void setLoop(int index, boolean enable) {
Message msg = new Message();
msg.what = VideoTaskSetLoop;
msg.arg1 = index;
if (enable) {
msg.arg2 = 1;
} else {
msg.arg2 = 0;
}
mVideoHandler.sendMessage(msg);
}

private void _setLoop(int index, boolean enable) {
CocosVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
videoView.setLoop(enable);
}
}

private void _setVideoKeepRatio(int index, boolean enable) {
CocosVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@

package com.cocos.lib;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;
import android.app.Activity;
import android.graphics.Point;

import java.io.IOException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -162,6 +163,33 @@ public void setKeepRatio(boolean enabled) {
fixSize();
}

public void setPlaybackRate(float value) {
if (mMediaPlayer != null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
PlaybackParams params = new PlaybackParams();
params.setSpeed(value);
mMediaPlayer.setPlaybackParams(params);
}
}
}

public void setMute(boolean enabled) {
if (mMediaPlayer != null) {
if (enabled) {
mMediaPlayer.setVolume(0f, 0f);
} else {
mMediaPlayer.setVolume(1f, 1f);
}
}
}

public void setLoop(boolean enabled) {
if (mMediaPlayer != null) {
mMediaPlayer.setLooping(enabled);
}
}


public void setVideoURL(String url) {
mIsAssetResource = false;
setVideoURI(Uri.parse(url), null);
Expand Down
55 changes: 55 additions & 0 deletions native/cocos/ui/videoplayer/VideoPlayer-ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ - (float)currentTime;
- (float)duration;
- (void)setVisible:(BOOL)visible;
- (void)setKeepRatioEnabled:(BOOL)enabled;
- (void)setPlaybackRate:(float)value;
- (void)setMute:(BOOL)enabled;
- (void)setLoop:(BOOL)enabled;
- (void)setFullScreenEnabled:(BOOL)enabled;
- (void)showPlaybackControls:(BOOL)value;
- (BOOL)isFullScreenEnabled;
Expand All @@ -78,6 +81,7 @@ @implementation UIVideoViewWrapperIos {
int _width;
int _height;
bool _keepRatioEnabled;
bool _keepLoopEnabled;
bool _fullscreen;
CGRect _restoreRect;
PlayerbackState _state;
Expand All @@ -87,6 +91,7 @@ @implementation UIVideoViewWrapperIos {
- (id)init:(void *)videoPlayer {
if (self = [super init]) {
_keepRatioEnabled = FALSE;
_keepLoopEnabled = FALSE;
_left = _top = _width = _height = 0;

[self initPlayerController];
Expand Down Expand Up @@ -182,6 +187,21 @@ - (void)setKeepRatioEnabled:(BOOL)enabled {
self.playerController.videoGravity = AVLayerVideoGravityResize;
}

- (void)setMute:(BOOL)enabled {
[self.playerController.player setMuted:enabled];
}

- (void)setLoop:(BOOL)enabled {
_keepLoopEnabled = enabled;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheVideo:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerController.player.currentItem];
}

- (void)setPlaybackRate:(float)value {
if (self.playerController.player) {
[self.playerController.player playImmediatelyAtRate:value];
}
}

- (void)play {
if (self.playerController.player && ![self isPlaying]) {
[self.playerController.player play];
Expand All @@ -190,6 +210,14 @@ - (void)play {
}
}

- (void)runLoopTheVideo:(NSNotification *)notification {
if (_keepLoopEnabled) {
AVPlayerItem *playerItem = notification.object;
[self seekTo:0];
[self.playerController.player play];
}
}

- (void)pause {
if ([self isPlaying]) {
[self.playerController.player pause];
Expand Down Expand Up @@ -331,6 +359,33 @@ - (void)addPlayerControllerSubView {
}
}

void VideoPlayer::setMute(bool enable) {
if (!_videoURL.empty()) {
if (enable) {
[((UIVideoViewWrapperIos *)_videoView) setMute:YES];
} else {
[((UIVideoViewWrapperIos *)_videoView) setMute:NO];
}
}
}

void VideoPlayer::setLoop(bool enable) {
if (!_videoURL.empty()) {
if (enable) {
[((UIVideoViewWrapperIos *)_videoView) setLoop:YES];
}
else {
[((UIVideoViewWrapperIos *)_videoView) setLoop:NO];
}
}
}

void VideoPlayer::setPlaybackRate(float value) {
if (!_videoURL.empty()) {
[((UIVideoViewWrapperIos *)_videoView) setPlaybackRate:value];
}
}

void VideoPlayer::play() {
if (!_videoURL.empty() && _isVisible) {
[((UIVideoViewWrapperIos *)_videoView) play];
Expand Down
14 changes: 13 additions & 1 deletion native/cocos/ui/videoplayer/VideoPlayer-java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,23 @@ void VideoPlayer::setURL(const ccstd::string &videoUrl) {
static_cast<int>(_videoSource), _videoURL);
}

void VideoPlayer::VideoPlayer::setFrame(float x, float y, float width, float height) {
void VideoPlayer::setFrame(float x, float y, float width, float height) {
JniHelper::callStaticVoidMethod(VIDEO_HELPER_CLASS_NAME, "setVideoRect", _videoPlayerIndex,
static_cast<int>(x), static_cast<int>(y), static_cast<int>(width), static_cast<int>(height));
}

void VideoPlayer::setPlaybackRate(float value) {
JniHelper::callStaticVoidMethod(VIDEO_HELPER_CLASS_NAME, "setPlaybackRate", _videoPlayerIndex, value);
}

void VideoPlayer::setMute(bool enable) {
JniHelper::callStaticVoidMethod(VIDEO_HELPER_CLASS_NAME, "setMute", _videoPlayerIndex, enable);
}

void VideoPlayer::setLoop(bool enable) {
JniHelper::callStaticVoidMethod(VIDEO_HELPER_CLASS_NAME, "setLoop", _videoPlayerIndex, enable);
}

void VideoPlayer::setFullScreenEnabled(bool fullscreen) {
if (_fullScreenEnabled != fullscreen) {
_fullScreenEnabled = fullscreen;
Expand Down
15 changes: 15 additions & 0 deletions native/cocos/ui/videoplayer/VideoPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ class VideoPlayer final {
*/
virtual void setFrame(float x, float y, float width, float height);

/**
* Set playback rate of VideoPlayer.
*/
virtual void setPlaybackRate(float value);

/**
* Set mute of VideoPlayer.
*/
virtual void setMute(bool enable);

/**
* Set loop of VideoPlayer.
*/
virtual void setLoop(bool enable);

protected:
enum class Source {
FILENAME = 0,
Expand Down
18 changes: 12 additions & 6 deletions platforms/native/engine/jsb-videoplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,26 @@ if (cc.internal.VideoPlayer) {
return this.video.duration();
}

syncPlaybackRate() {
cc.warn('The platform does not support');
syncPlaybackRate(value) {
if (this.video) {
this.video.setPlaybackRate(value);
}
}

syncVolume() {
cc.warn('The platform does not support');
}

syncMute() {
cc.warn('The platform does not support');
syncMute(enable) {
if (this.video && this.video.muted !== enable) {
this.video.setMute(enable);
}
}

syncLoop() {
cc.warn('The platform does not support');
syncLoop(enable) {
if (this.video && this.video.loop !== enable) {
this.video.setLoop(enable);
}
}

syncStayOnBottom() {
Expand Down

0 comments on commit 40ea3c0

Please sign in to comment.