Skip to content

Commit 6805c75

Browse files
committed
Fix surface view not resizing video correctly
Also fix yet another random null pointer exception that could happen when adding the video player view
1 parent 75917c7 commit 6805c75

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ private void openMainPlayer() {
12201220
}
12211221

12221222
final PlayQueue queue = setupPlayQueueForIntent(false);
1223-
addVideoPlayerView();
1223+
tryAddVideoPlayerView();
12241224

12251225
final Intent playerIntent = NavigationHelper.getPlayerIntent(requireContext(),
12261226
PlayerService.class, queue, true, autoPlayEnabled);
@@ -1301,21 +1301,27 @@ private boolean isAutoplayEnabled() {
13011301
&& PlayerHelper.isAutoplayAllowedByUser(requireContext());
13021302
}
13031303

1304-
private void addVideoPlayerView() {
1305-
if (!isPlayerAvailable() || getView() == null) {
1306-
return;
1307-
}
1308-
setHeightThumbnail();
1304+
private void tryAddVideoPlayerView() {
1305+
// do all the null checks in the posted lambda, since the player, the binding and the view
1306+
// could be set or unset before the lambda gets executed on the next main thread cycle
1307+
new Handler(Looper.getMainLooper()).post(() -> {
1308+
if (!isPlayerAvailable() || getView() == null) {
1309+
return;
1310+
}
13091311

1310-
// Prevent from re-adding a view multiple times
1311-
new Handler(Looper.getMainLooper()).post(() ->
1312-
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
1313-
if (binding != null) {
1314-
playerUi.removeViewFromParent();
1315-
binding.playerPlaceholder.addView(playerUi.getBinding().getRoot());
1316-
playerUi.setupVideoSurfaceIfNeeded();
1317-
}
1318-
}));
1312+
// setup the surface view height, so that it fits the video correctly
1313+
setHeightThumbnail();
1314+
1315+
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
1316+
// sometimes binding would be null here, even though getView() != null above u.u
1317+
if (binding != null) {
1318+
// prevent from re-adding a view multiple times
1319+
playerUi.removeViewFromParent();
1320+
binding.playerPlaceholder.addView(playerUi.getBinding().getRoot());
1321+
playerUi.setupVideoSurfaceIfNeeded();
1322+
}
1323+
});
1324+
});
13191325
}
13201326

13211327
private void removeVideoPlayerView() {
@@ -1784,7 +1790,7 @@ private void showPlaybackProgress(final long progress, final long duration) {
17841790

17851791
@Override
17861792
public void onViewCreated() {
1787-
addVideoPlayerView();
1793+
tryAddVideoPlayerView();
17881794
}
17891795

17901796
@Override
@@ -1926,7 +1932,7 @@ public void onFullscreenStateChanged(final boolean fullscreen) {
19261932
}
19271933
scrollToTop();
19281934

1929-
addVideoPlayerView();
1935+
tryAddVideoPlayerView();
19301936
}
19311937

19321938
@Override

0 commit comments

Comments
 (0)