Skip to content

Commit dd39316

Browse files
mensononesbehenate
andauthored
[video][android] Implement exitFullscreen (#41836)
# Why Fixes #41833 Currently, calling `exitFullscreen()` on Android throws a `MethodUnsupportedException`. This PR implements the functionality to programmatically exit fullscreen mode. # How - Implemented `exitFullscreen` in `VideoModule` by calling `VideoManager.finishFullscreenPlayer`. - Added `finishFullscreenPlayer` to `VideoManager` to finish the activity. - Updated `VideoManager.unregisterFullscreenPlayerActivity` to verify the activity instance to prevent race conditions. - Updated `FullscreenPlayerActivity` to correctly attach the player back to the VideoView on finish and handle cleanup in `onDestroy`. - Added a try-catch block in `FullscreenPlayerActivity.finish()` to handle cases where the original VideoView might have been destroyed. # Test Plan - Tested in a local reproduction app. - Verified that calling `exitFullscreen()` closes the fullscreen player. - Verified that the player is correctly re-attached to the inline view. - Verified that no crash occurs if the inline view is destroyed while in fullscreen. <!-- Please describe how you tested this change and how a reviewer could reproduce your test, especially if this PR does not include automated tests! If possible, please also provide terminal output and/or screenshots demonstrating your test/reproduction. --> # Checklist <!-- Please check the appropriate items below if they apply to your diff. --> - [x] I added a `changelog.md` entry and rebuilt the package sources according to [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) --------- Co-authored-by: Wojciech Dróżdż <[email protected]>
1 parent abc4ed5 commit dd39316

9 files changed

Lines changed: 80 additions & 8 deletions

File tree

‎packages/expo-video/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### 🎉 New features
88

9+
- [Android] Implement `exitFullscreen` to fix `MethodUnsupportedException`. ([#41836](https://github.com/expo/expo/pull/41836) by [@mensonones](https://github.com/mensonones))
10+
911
### 🐛 Bug fixes
1012

1113
### 💡 Others

‎packages/expo-video/android/src/main/java/expo/modules/video/FullscreenPlayerActivity.kt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class FullscreenPlayerActivity : Activity(), VideoManagerListener {
8686
videoPlayer?.hasBeenDisconnectedFromVideoView() // The video player is disconnected. We are only using the ExoPlayer it contained
8787
}
8888

89-
VideoManager.registerFullscreenPlayerActivity(hashCode().toString(), this)
89+
videoViewId?.let { VideoManager.registerFullscreenPlayerActivity(it, this) }
9090
VideoManager.registerListener(this)
9191
playerView.player?.let {
9292
val aspectRatio = calculatePiPAspectRatio(it.videoSize, playerView.width, playerView.height, videoView.contentFit)
@@ -180,9 +180,11 @@ class FullscreenPlayerActivity : Activity(), VideoManagerListener {
180180
captioningChangeListener = null
181181
}
182182

183-
videoView.exitFullscreen()
183+
if (::videoView.isInitialized) {
184+
videoView.exitFullscreen()
185+
}
184186
VideoManager.unregisterListener(this)
185-
VideoManager.unregisterFullscreenPlayerActivity(hashCode().toString())
187+
videoViewId?.let { VideoManager.unregisterFullscreenPlayerActivity(it) }
186188
orientationHelper?.stopOrientationEventListener()
187189
}
188190

‎packages/expo-video/android/src/main/java/expo/modules/video/VideoModule.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,9 @@ private inline fun <reified T : VideoView> ViewDefinitionBuilder<T>.VideoViewCom
420420
AsyncFunction("enterFullscreen") { view: T ->
421421
view.enterFullscreen()
422422
}.runOnQueue(Queues.MAIN)
423-
AsyncFunction("exitFullscreen") {
424-
throw MethodUnsupportedException("exitFullscreen")
425-
}
423+
AsyncFunction("exitFullscreen") { view: T ->
424+
VideoManager.finishFullscreenPlayer(view.videoViewId)
425+
}.runOnQueue(Queues.MAIN)
426426
AsyncFunction("startPictureInPicture") { view: T ->
427427
runWithPiPMisconfigurationSoftHandling(true) {
428428
view.enterPictureInPicture()

‎packages/expo-video/android/src/main/java/expo/modules/video/managers/VideoManager.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ object VideoManager {
101101
fullscreenPlayerActivities.remove(id)
102102
}
103103

104+
fun finishFullscreenPlayer(id: String) {
105+
val activityRef = fullscreenPlayerActivities[id]
106+
activityRef?.get()?.finish()
107+
}
108+
104109
fun onVideoPlayerAttachedToView(videoPlayer: VideoPlayer, videoView: VideoView) {
105110
if (videoPlayersToVideoViews[videoPlayer]?.contains(videoView) == true) {
106111
return

‎packages/expo-video/build/VideoView.d.ts‎

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/expo-video/build/VideoView.d.ts.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/expo-video/build/VideoView.js‎

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/expo-video/build/VideoView.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/expo-video/src/VideoView.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ export class VideoView extends PureComponent<VideoViewProps> {
3535

3636
/**
3737
* Exits fullscreen mode.
38+
*
39+
* > **Note:** On Android the JS runtime is paused when the `VideoView` is in fullscreen mode. Because of this `exitFullscreen` will only work when called from a native listener - see the example below.
40+
*
41+
* @example
42+
* ```tsx
43+
* const ref = useRef<VideoView>()
44+
* const player = useVideoPlayer(source)
45+
*
46+
* // This will work on all platforms
47+
* useEventListener(player, 'playToEnd', () => {
48+
* ref.current?.exitFullscreen();
49+
* });
50+
*
51+
* // This will not work on Android
52+
* const enterAndExit = useCallback(() => {
53+
* setTimeout(() => {
54+
* ref.current?.exitFullscreen()
55+
* }, 5000)
56+
* ref.current?.enterFullscreen()
57+
* },[])
58+
* ```
3859
*/
3960
async exitFullscreen(): Promise<void> {
4061
return await this.nativeRef.current?.exitFullscreen();

0 commit comments

Comments
 (0)