Skip to content

Commit 6efa04b

Browse files
[camera,video_player] replace onSurfaceDestroyed with onSurfaceCleanup (#9316)
Replaces deprecated `onSurfaceDestroyed` with `onSurfaceCleanup`, which became available in Flutter 3.29 (which these packages require as of #9317). The changes to video player are non-trivial because rather than make the minimal change, this makes the changes discussed in flutter/flutter#160933 (comment) that motivated the change in API in the first place, which simplifies the handling of backgrounding, and restores background playback. Fixes flutter/flutter#161256 Fixes flutter/flutter#156936 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent c33fa39 commit 6efa04b

File tree

15 files changed

+61
-200
lines changed

15 files changed

+61
-200
lines changed

packages/camera/camera_android_camerax/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.17+1
2+
3+
* Replaces deprecated `onSurfaceDestroyed` with `onSurfaceCleanup`.
4+
15
## 0.6.17
26

37
* Replaces `BroadcastReceiver` usage with an `OrientationEventListener` to detect changes in device

packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/PreviewProxyApi.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ public void onSurfaceAvailable() {
109109
}
110110

111111
@Override
112-
// TODO(bparrishMines): Replace with onSurfaceCleanup once available on stable. See
113-
// https://github.com/flutter/flutter/issues/161256.
114-
@SuppressWarnings({"deprecation", "removal"})
115-
public void onSurfaceDestroyed() {
112+
public void onSurfaceCleanup() {
116113
// Invalidate the SurfaceRequest so that CameraX knows to to make a new request
117114
// for a surface.
118115
request.invalidate();

packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/PreviewTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ TextureRegistry getTextureRegistry() {
113113

114114
final TextureRegistry.SurfaceProducer.Callback callback = callbackCaptor.getValue();
115115

116-
// Verify callback's onSurfaceDestroyed invalidates SurfaceRequest.
117-
simulateSurfaceDestruction(callback);
116+
// Verify callback's onSurfaceCleanup invalidates SurfaceRequest.
117+
simulateSurfaceCleanup(callback);
118118
verify(mockSurfaceRequest).invalidate();
119119

120120
reset(mockSurfaceRequest);
@@ -286,7 +286,7 @@ TextureRegistry getTextureRegistry() {
286286
// see https://github.com/flutter/flutter/issues/16125. This separate method only exists to scope
287287
// the suppression.
288288
@SuppressWarnings({"deprecation", "removal"})
289-
void simulateSurfaceDestruction(TextureRegistry.SurfaceProducer.Callback producerLifecycle) {
290-
producerLifecycle.onSurfaceDestroyed();
289+
void simulateSurfaceCleanup(TextureRegistry.SurfaceProducer.Callback producerLifecycle) {
290+
producerLifecycle.onSurfaceCleanup();
291291
}
292292
}

packages/camera/camera_android_camerax/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_android_camerax
22
description: Android implementation of the camera plugin using the CameraX library.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.6.17
5+
version: 0.6.17+1
66

77
environment:
88
sdk: ^3.7.0

packages/video_player/video_player_android/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.8.5
2+
3+
* Replaces deprecated `onSurfaceDestroyed` with `onSurfaceCleanup`.
4+
* Restores background playback support.
5+
16
## 2.8.4
27

38
* Fixes incorrect width/height swap ([bug](https://github.com/flutter/flutter/issues/166097)). The swap was originally required for the uncorrected width/height of `Format` but was mistakenly retained after [switching to `VideoSize`](https://github.com/flutter/packages/pull/6535), which already accounts for rotation.

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/ExoPlayerEventListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public abstract class ExoPlayerEventListener implements Player.Listener {
1313
private boolean isBuffering = false;
14-
private boolean isInitialized;
14+
private boolean isInitialized = false;
1515
protected final ExoPlayer exoPlayer;
1616
protected final VideoPlayerCallbacks events;
1717

@@ -42,10 +42,9 @@ public int getDegrees() {
4242
}
4343

4444
public ExoPlayerEventListener(
45-
@NonNull ExoPlayer exoPlayer, @NonNull VideoPlayerCallbacks events, boolean initialized) {
45+
@NonNull ExoPlayer exoPlayer, @NonNull VideoPlayerCallbacks events) {
4646
this.exoPlayer = exoPlayer;
4747
this.events = events;
48-
this.isInitialized = initialized;
4948
}
5049

5150
private void setBuffering(boolean buffering) {

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/ExoPlayerState.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayer.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
* <p>It provides methods to control playback, adjust volume, and handle seeking.
2323
*/
2424
public abstract class VideoPlayer {
25-
@NonNull private final ExoPlayerProvider exoPlayerProvider;
26-
@NonNull private final MediaItem mediaItem;
27-
@NonNull private final VideoPlayerOptions options;
2825
@NonNull protected final VideoPlayerCallbacks videoPlayerEvents;
2926
@Nullable protected final SurfaceProducer surfaceProducer;
3027
@NonNull protected ExoPlayer exoPlayer;
@@ -47,22 +44,12 @@ public VideoPlayer(
4744
@Nullable SurfaceProducer surfaceProducer,
4845
@NonNull ExoPlayerProvider exoPlayerProvider) {
4946
this.videoPlayerEvents = events;
50-
this.mediaItem = mediaItem;
51-
this.options = options;
52-
this.exoPlayerProvider = exoPlayerProvider;
5347
this.surfaceProducer = surfaceProducer;
54-
this.exoPlayer = createVideoPlayer();
55-
}
56-
57-
@NonNull
58-
protected ExoPlayer createVideoPlayer() {
59-
ExoPlayer exoPlayer = exoPlayerProvider.get();
48+
exoPlayer = exoPlayerProvider.get();
6049
exoPlayer.setMediaItem(mediaItem);
6150
exoPlayer.prepare();
6251
exoPlayer.addListener(createExoPlayerEventListener(exoPlayer, surfaceProducer));
6352
setAudioAttributes(exoPlayer, options.mixWithOthers);
64-
65-
return exoPlayer;
6653
}
6754

6855
@NonNull

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/platformview/PlatformViewExoPlayerEventListener.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import androidx.annotation.NonNull;
88
import androidx.annotation.OptIn;
9-
import androidx.annotation.VisibleForTesting;
109
import androidx.media3.common.Format;
1110
import androidx.media3.common.util.UnstableApi;
1211
import androidx.media3.exoplayer.ExoPlayer;
@@ -15,15 +14,9 @@
1514
import java.util.Objects;
1615

1716
public final class PlatformViewExoPlayerEventListener extends ExoPlayerEventListener {
18-
@VisibleForTesting
1917
public PlatformViewExoPlayerEventListener(
2018
@NonNull ExoPlayer exoPlayer, @NonNull VideoPlayerCallbacks events) {
21-
this(exoPlayer, events, false);
22-
}
23-
24-
public PlatformViewExoPlayerEventListener(
25-
@NonNull ExoPlayer exoPlayer, @NonNull VideoPlayerCallbacks events, boolean initialized) {
26-
super(exoPlayer, events, initialized);
19+
super(exoPlayer, events);
2720
}
2821

2922
@OptIn(markerClass = UnstableApi.class)

packages/video_player/video_player_android/android/src/main/java/io/flutter/plugins/videoplayer/platformview/PlatformViewVideoPlayer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public static PlatformViewVideoPlayer create(
6262
@Override
6363
protected ExoPlayerEventListener createExoPlayerEventListener(
6464
@NonNull ExoPlayer exoPlayer, @Nullable SurfaceProducer surfaceProducer) {
65-
// Platform view video player does not suspend and re-create the exoPlayer, hence initialized
66-
// is always false. It also does not require a reference to the SurfaceProducer.
67-
return new PlatformViewExoPlayerEventListener(exoPlayer, videoPlayerEvents, false);
65+
return new PlatformViewExoPlayerEventListener(exoPlayer, videoPlayerEvents);
6866
}
6967
}

0 commit comments

Comments
 (0)