Skip to content
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

feat(android): add onControlsVisiblityChange #3925

Merged
merged 5 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -37,6 +37,7 @@ public VideoEventEmitter(ReactContext reactContext) {
private static final String EVENT_ERROR = "onVideoError";
private static final String EVENT_PROGRESS = "onVideoProgress";
private static final String EVENT_BANDWIDTH = "onVideoBandwidthUpdate";
private static final String EVENT_CONTROLS_VISIBILITY_CHANGE = "onControlsVisibilityChange";
private static final String EVENT_SEEK = "onVideoSeek";
private static final String EVENT_END = "onVideoEnd";
private static final String EVENT_FULLSCREEN_WILL_PRESENT = "onVideoFullscreenPlayerWillPresent";
Expand Down Expand Up @@ -89,6 +90,7 @@ public VideoEventEmitter(ReactContext reactContext) {
EVENT_TEXT_TRACK_DATA_CHANGED,
EVENT_VIDEO_TRACKS,
EVENT_BANDWIDTH,
EVENT_CONTROLS_VISIBILITY_CHANGE,
EVENT_ON_RECEIVE_AD_EVENT
};

Expand Down Expand Up @@ -120,6 +122,7 @@ public VideoEventEmitter(ReactContext reactContext) {
EVENT_TEXT_TRACK_DATA_CHANGED,
EVENT_VIDEO_TRACKS,
EVENT_BANDWIDTH,
EVENT_CONTROLS_VISIBILITY_CHANGE,
EVENT_ON_RECEIVE_AD_EVENT
})
@interface VideoEvents {
Expand Down Expand Up @@ -164,6 +167,8 @@ public VideoEventEmitter(ReactContext reactContext) {

private static final String EVENT_PROP_IS_PLAYING = "isPlaying";

private static final String EVENT_CONTROLS_VISIBLE = "isVisible";

public void setViewId(int viewId) {
this.viewId = viewId;
}
Expand Down Expand Up @@ -354,6 +359,12 @@ public void end() {
receiveEvent(EVENT_END, null);
}

public void controlsVisibilityChanged(boolean isVisible) {
WritableMap map = Arguments.createMap();
map.putBoolean(EVENT_CONTROLS_VISIBLE, isVisible);
receiveEvent(EVENT_CONTROLS_VISIBILITY_CHANGE, map);
}

public void fullscreenWillPresent() {
receiveEvent(EVENT_FULLSCREEN_WILL_PRESENT, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ private void togglePlayerControlVisibility() {
private void initializePlayerControl() {
if (playerControlView == null) {
playerControlView = new LegacyPlayerControlView(getContext());
playerControlView.addVisibilityListener(new LegacyPlayerControlView.VisibilityListener() {
@Override
public void onVisibilityChange(int visibility) {
eventEmitter.controlsVisibilityChanged(visibility == View.VISIBLE);
}
});
}

if (fullScreenPlayerView == null) {
Expand Down
20 changes: 20 additions & 0 deletions docs/pages/component/events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ Example:
}
```

### `onControlsVisibilityChange`

<PlatformsList types={['Android']} />

Callback function that is called when the controls are hidden or shown. Not possible on iOS.

Payload:

| Property | Type | Description |
| ----------- | ------- | ---------------------------------------------- |
| isVisible | boolean | Boolean indicating whether controls are visible |

Example:

```javascript
{
isVisible: true;
}
```

### `onEnd`

<PlatformsList types={['All']} />
Expand Down
28 changes: 21 additions & 7 deletions src/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
type OnAudioTracksData,
type OnBandwidthUpdateData,
type OnBufferData,
type OnControlsVisibilityChange,
type OnExternalPlaybackChangeData,
type OnGetLicenseData,
type OnLoadStartData,
Expand Down Expand Up @@ -91,6 +92,7 @@
onEnd,
onBuffer,
onBandwidthUpdate,
onControlsVisibilityChange,
onExternalPlaybackChange,
onFullscreenPlayerWillPresent,
onFullscreenPlayerDidPresent,
Expand Down Expand Up @@ -391,13 +393,6 @@
[onVideoTracks],
);

const _onPlaybackRateChange = useCallback(
(e: NativeSyntheticEvent<Readonly<{playbackRate: number}>>) => {
onPlaybackRateChange?.(e.nativeEvent);
},
[onPlaybackRateChange],
);

const _onVolumeChange = useCallback(
(e: NativeSyntheticEvent<Readonly<{volume: number}>>) => {
onVolumeChange?.(e.nativeEvent);
Expand Down Expand Up @@ -459,6 +454,20 @@
[onAspectRatio],
);

const _onPlaybackRateChange = useCallback(
freeboub marked this conversation as resolved.
Show resolved Hide resolved
(e: NativeSyntheticEvent<Readonly<{playbackRate: number}>>) => {
onPlaybackRateChange?.(e.nativeEvent);
},
[onPlaybackRateChange],
);

const _onControlsVisibilityChange = useCallback(
(e: NativeSyntheticEvent<OnControlsVisibilityChange>) => {
onControlsVisibilityChange?.(e.nativeEvent);
},
[onControlsVisibilityChange],
);

const useExternalGetLicense = drm?.getLicense instanceof Function;

const onGetLicense = useCallback(
Expand Down Expand Up @@ -616,6 +625,11 @@
? (_onReceiveAdEvent as (e: NativeSyntheticEvent<object>) => void)
: undefined
}
onControlsVisibilityChange={
onControlsVisibilityChange
? _onControlsVisibilityChange
: undefined
}
/>
{hasPoster && showPoster ? (
<Image style={posterStyle} source={{uri: poster}} />
Expand Down
5 changes: 5 additions & 0 deletions src/specs/VideoNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ type ControlsStyles = Readonly<{
seekIncrementMS?: number;
}>;

export type OnControlsVisibilityChange = Readonly<{
isVisible: boolean;
}>;

export interface VideoNativeProps extends ViewProps {
src?: VideoSrc;
drm?: Drm;
Expand Down Expand Up @@ -337,6 +341,7 @@ export interface VideoNativeProps extends ViewProps {
useSecureView?: boolean; // Android
bufferingStrategy?: BufferingStrategyType; // Android
controlsStyles?: ControlsStyles; // Android
onControlsVisibilityChange?: DirectEventHandler<OnControlsVisibilityChange>;
onVideoLoad?: DirectEventHandler<OnLoadData>;
onVideoLoadStart?: DirectEventHandler<OnLoadStartData>;
onVideoAspectRatio?: DirectEventHandler<OnVideoAspectRatioData>;
Expand Down
2 changes: 2 additions & 0 deletions src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
OnAudioTracksData,
OnBandwidthUpdateData,
OnBufferData,
OnControlsVisibilityChange,
OnExternalPlaybackChangeData,
OnLoadStartData,
OnPictureInPictureStatusChangedData,
Expand Down Expand Up @@ -237,6 +238,7 @@ export interface ReactVideoEvents {
onIdle?: () => void; // Android
onBandwidthUpdate?: (e: OnBandwidthUpdateData) => void; //Android
onBuffer?: (e: OnBufferData) => void; //Android, iOS
onControlsVisibilityChange?: (e: OnControlsVisibilityChange) => void; // Android, iOS
onEnd?: () => void; //All
onError?: (e: OnVideoErrorData) => void; //Android, iOS
onExternalPlaybackChange?: (e: OnExternalPlaybackChangeData) => void; //iOS
Expand Down
Loading