Fix surface view not resizing video correctly

Also fix yet another random null pointer exception that could happen when adding the video player view
This commit is contained in:
Stypox 2022-08-02 14:46:11 +02:00
parent 75917c7f61
commit 6805c75c9c
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 23 additions and 17 deletions

View File

@ -1220,7 +1220,7 @@ public final class VideoDetailFragment
} }
final PlayQueue queue = setupPlayQueueForIntent(false); final PlayQueue queue = setupPlayQueueForIntent(false);
addVideoPlayerView(); tryAddVideoPlayerView();
final Intent playerIntent = NavigationHelper.getPlayerIntent(requireContext(), final Intent playerIntent = NavigationHelper.getPlayerIntent(requireContext(),
PlayerService.class, queue, true, autoPlayEnabled); PlayerService.class, queue, true, autoPlayEnabled);
@ -1301,21 +1301,27 @@ public final class VideoDetailFragment
&& PlayerHelper.isAutoplayAllowedByUser(requireContext()); && PlayerHelper.isAutoplayAllowedByUser(requireContext());
} }
private void addVideoPlayerView() { private void tryAddVideoPlayerView() {
if (!isPlayerAvailable() || getView() == null) { // do all the null checks in the posted lambda, since the player, the binding and the view
return; // could be set or unset before the lambda gets executed on the next main thread cycle
} new Handler(Looper.getMainLooper()).post(() -> {
setHeightThumbnail(); if (!isPlayerAvailable() || getView() == null) {
return;
}
// Prevent from re-adding a view multiple times // setup the surface view height, so that it fits the video correctly
new Handler(Looper.getMainLooper()).post(() -> setHeightThumbnail();
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
if (binding != null) { player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
playerUi.removeViewFromParent(); // sometimes binding would be null here, even though getView() != null above u.u
binding.playerPlaceholder.addView(playerUi.getBinding().getRoot()); if (binding != null) {
playerUi.setupVideoSurfaceIfNeeded(); // prevent from re-adding a view multiple times
} playerUi.removeViewFromParent();
})); binding.playerPlaceholder.addView(playerUi.getBinding().getRoot());
playerUi.setupVideoSurfaceIfNeeded();
}
});
});
} }
private void removeVideoPlayerView() { private void removeVideoPlayerView() {
@ -1784,7 +1790,7 @@ public final class VideoDetailFragment
@Override @Override
public void onViewCreated() { public void onViewCreated() {
addVideoPlayerView(); tryAddVideoPlayerView();
} }
@Override @Override
@ -1926,7 +1932,7 @@ public final class VideoDetailFragment
} }
scrollToTop(); scrollToTop();
addVideoPlayerView(); tryAddVideoPlayerView();
} }
@Override @Override