Skip to content

Commit

Permalink
Fix video playback starting when off screen in media viewer.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal authored and greyson-signal committed Dec 30, 2022
1 parent ebdfa88 commit 055b469
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,6 @@ public abstract class MediaPreviewFragment extends Fragment {
private AttachmentId attachmentId;
protected Events events;

public static MediaPreviewFragment newInstance(@NonNull Attachment attachment, boolean autoPlay) {
return newInstance(attachment.getUri(), attachment.getContentType(), attachment.getSize(), autoPlay, attachment.isVideoGif());
}

public static MediaPreviewFragment newInstance(@NonNull Uri dataUri, @NonNull String contentType, long size, boolean autoPlay, boolean isVideoGif) {
Bundle args = new Bundle();

args.putParcelable(MediaPreviewFragment.DATA_URI, dataUri);
args.putString(MediaPreviewFragment.DATA_CONTENT_TYPE, contentType);
args.putLong(MediaPreviewFragment.DATA_SIZE, size);
args.putBoolean(MediaPreviewFragment.AUTO_PLAY, autoPlay);
args.putBoolean(MediaPreviewFragment.VIDEO_GIF, isVideoGif);

MediaPreviewFragment fragment = createCorrectFragmentType(contentType);

fragment.setArguments(args);

return fragment;
}

private static MediaPreviewFragment createCorrectFragmentType(@NonNull String contentType) {
if (MediaUtil.isVideo(contentType)) {
return new VideoMediaPreviewFragment();
} else if (MediaUtil.isImageType(contentType)) {
return new ImageMediaPreviewFragment();
} else {
throw new AssertionError("Unexpected media type: " + contentType);
}
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Expand All @@ -78,6 +48,7 @@ public void onResume() {
checkMediaStillAvailable();
}

public void autoPlayIfNeeded() {}
public abstract void cleanUp();
public abstract void pause();
public abstract void setBottomButtonControls(MediaPreviewPlayerControlView playerControlView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MediaPreviewV2Adapter(fragment: Fragment) : FragmentStateAdapter(fragment)
MediaPreviewFragment.DATA_URI to attachment.uri,
MediaPreviewFragment.DATA_CONTENT_TYPE to contentType,
MediaPreviewFragment.DATA_SIZE to attachment.size,
MediaPreviewFragment.AUTO_PLAY to true,
MediaPreviewFragment.AUTO_PLAY to attachment.isVideoGif,
MediaPreviewFragment.VIDEO_GIF to attachment.isVideoGif,
)
val fragment = if (MediaUtil.isVideo(contentType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class MediaPreviewV2Fragment : LoggingFragment(R.layout.fragment_media_preview_v
forward(currentItem)
}
currentFragment?.setBottomButtonControls(binding.mediaPreviewPlaybackControls)
currentFragment?.autoPlayIfNeeded()
}

private fun bindAlbumRail(albumThumbnailMedia: List<Media>, currentItem: MediaTable.MediaRecord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ public final class VideoMediaPreviewFragment extends MediaPreviewFragment {
private boolean isVideoGif;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View itemView = inflater.inflate(R.layout.media_preview_video_fragment, container, false);
Bundle arguments = requireArguments();
Uri uri = arguments.getParcelable(DATA_URI);
Expand Down Expand Up @@ -111,13 +104,6 @@ public void onDestroyView() {
cleanUp();
}

@Override
public void cleanUp() {
if (videoView != null) {
videoView.cleanup();
}
}

@Override
public void onResume() {
super.onResume();
Expand All @@ -131,6 +117,20 @@ public void onResume() {
}
}

@Override
public void autoPlayIfNeeded() {
if (videoView != null && videoView.getPlaybackPosition() < videoView.getDuration()) {
videoView.play();
}
}

@Override
public void cleanUp() {
if (videoView != null) {
videoView.cleanup();
}
}

@Override
public void pause() {
if (videoView != null) {
Expand Down

0 comments on commit 055b469

Please sign in to comment.