Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add video autoplay to media gallery #4499

Merged
merged 6 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.element.android.libraries.mediaviewer.impl.model.GroupedMediaItems
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
Expand Down Expand Up @@ -85,11 +86,13 @@ class TimelineMediaGalleryDataSource @Inject constructor(
}
}.flatMapLatest {
timelineMediaItemsFactory.timelineItems
}.map { timelineItems ->
mediaItemsPostProcessor.process(mediaItems = timelineItems)
}.map {
mediaTimeline.orCache(it)
}.onEach { groupedMediaItems ->
}
.distinctUntilChanged()
.map { timelineItems ->
val groupedItems = mediaItemsPostProcessor.process(mediaItems = timelineItems)
mediaTimeline.orCache(groupedItems)
}
.onEach { groupedMediaItems ->
Comment on lines +89 to +95
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No actual changes here, but Detekt complained about this so I made some changes to make it happy.

groupedMediaItemsFlow.emit(AsyncData.Success(groupedMediaItems))
}
.onCompletion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fun LocalMediaView(
textFileViewer: TextFileViewer,
modifier: Modifier = Modifier,
isDisplayed: Boolean = true,
isUserSelected: Boolean = false,
localMediaViewState: LocalMediaViewState = rememberLocalMediaViewState(),
mediaInfo: MediaInfo? = localMedia?.info,
) {
Expand All @@ -47,6 +48,7 @@ fun LocalMediaView(
localMediaViewState = localMediaViewState,
bottomPaddingInPixels = bottomPaddingInPixels,
localMedia = localMedia,
autoplay = isUserSelected,
modifier = modifier,
)
mimeType == MimeTypes.PlainText -> TextFileView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private fun ExoPlayerMediaAudioView(
MediaPlayerControllerState(
isVisible = true,
isPlaying = false,
isReady = false,
progressInMillis = 0,
durationInMillis = 0,
canMute = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.annotation.FloatRange
data class MediaPlayerControllerState(
val isVisible: Boolean,
val isPlaying: Boolean,
val isReady: Boolean,
val progressInMillis: Long,
val durationInMillis: Long,
val canMute: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ open class MediaPlayerControllerStateProvider : PreviewParameterProvider<MediaPl
private fun aMediaPlayerControllerState(
isVisible: Boolean = true,
isPlaying: Boolean = false,
isReady: Boolean = false,
progressInMillis: Long = 0,
// Default to 1 minute and 23 seconds
durationInMillis: Long = 83_000,
Expand All @@ -35,6 +36,7 @@ private fun aMediaPlayerControllerState(
) = MediaPlayerControllerState(
isVisible = isVisible,
isPlaying = isPlaying,
isReady = isReady,
progressInMillis = progressInMillis,
durationInMillis = durationInMillis,
canMute = canMute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.Player.STATE_READY
import androidx.media3.common.Timeline
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.AspectRatioFrameLayout
Expand Down Expand Up @@ -61,6 +62,7 @@ fun MediaVideoView(
localMediaViewState: LocalMediaViewState,
bottomPaddingInPixels: Int,
localMedia: LocalMedia?,
autoplay: Boolean,
modifier: Modifier = Modifier,
) {
val exoPlayer = rememberExoPlayer()
Expand All @@ -70,6 +72,7 @@ fun MediaVideoView(
bottomPaddingInPixels = bottomPaddingInPixels,
exoPlayer = exoPlayer,
localMedia = localMedia,
autoplay = autoplay,
modifier = modifier,
)
}
Expand All @@ -82,13 +85,15 @@ private fun ExoPlayerMediaVideoView(
bottomPaddingInPixels: Int,
exoPlayer: ExoPlayer,
localMedia: LocalMedia?,
autoplay: Boolean,
modifier: Modifier = Modifier,
) {
var mediaPlayerControllerState: MediaPlayerControllerState by remember {
mutableStateOf(
MediaPlayerControllerState(
isVisible = true,
isPlaying = false,
isReady = false,
progressInMillis = 0,
durationInMillis = 0,
canMute = true,
Expand Down Expand Up @@ -135,6 +140,12 @@ private fun ExoPlayerMediaVideoView(
}
}
}

override fun onPlaybackStateChanged(playbackState: Int) {
mediaPlayerControllerState = mediaPlayerControllerState.copy(
isReady = playbackState == STATE_READY,
)
}
}
}

Expand Down Expand Up @@ -164,9 +175,17 @@ private fun ExoPlayerMediaVideoView(
)
}
}
LaunchedEffect(isDisplayed) {
// If not displayed, make sure to pause the video
if (!isDisplayed) {

var needsAutoPlay by remember { mutableStateOf(autoplay) }

LaunchedEffect(needsAutoPlay, isDisplayed, mediaPlayerControllerState.isReady) {
val isReadyAndNotPlaying = mediaPlayerControllerState.isReady && !mediaPlayerControllerState.isPlaying
if (needsAutoPlay && isDisplayed && isReadyAndNotPlaying) {
// When displayed, start autoplaying
exoPlayer.play()
needsAutoPlay = false
} else if (!isDisplayed && mediaPlayerControllerState.isPlaying) {
// If not displayed, make sure to pause the video
exoPlayer.pause()
}
}
Expand Down Expand Up @@ -259,5 +278,6 @@ internal fun MediaVideoViewPreview() = ElementPreview {
bottomPaddingInPixels = 0,
localMediaViewState = rememberLocalMediaViewState(),
localMedia = null,
autoplay = false,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MediaViewerDataSource(
}

@VisibleForTesting
fun dataFlow(): Flow<PersistentList<MediaViewerPageData>> {
internal fun dataFlow(): Flow<PersistentList<MediaViewerPageData>> {
return galleryDataSource.groupedMediaItemsFlow()
.map { groupedItems ->
when (groupedItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class MediaViewerPresenter @AssistedInject constructor(
}

return MediaViewerState(
initiallySelectedEventId = inputs.eventId,
listData = data.value,
currentIndex = currentIndex.intValue,
snackbarMessage = snackbarMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.element.android.libraries.mediaviewer.impl.details.MediaBottomSheetSta
import kotlinx.collections.immutable.ImmutableList

data class MediaViewerState(
val initiallySelectedEventId: EventId?,
val listData: ImmutableList<MediaViewerPageData>,
val currentIndex: Int,
val snackbarMessage: SnackbarMessage?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.libraries.architecture.AsyncData
import io.element.android.libraries.designsystem.components.media.aWaveForm
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.media.MediaSource
import io.element.android.libraries.matrix.api.timeline.Timeline
import io.element.android.libraries.mediaviewer.api.MediaInfo
Expand Down Expand Up @@ -202,6 +203,7 @@ fun aMediaViewerState(
mediaBottomSheetState: MediaBottomSheetState = MediaBottomSheetState.Hidden,
eventSink: (MediaViewerEvents) -> Unit = {},
) = MediaViewerState(
initiallySelectedEventId = EventId("\$a:b"),
listData = listData.toPersistentList(),
currentIndex = currentIndex,
snackbarMessage = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,13 @@ fun MediaViewerView(
Box(
modifier = Modifier.fillMaxSize()
) {
val isDisplayed = remember(pagerState.settledPage) {
// This 'item provider' lambda will be called when the data source changes with an outdated `settlePage` value
// So we need to update this value only when the `settledPage` value changes. It seems like a bug that needs to be fixed in Compose.
page == pagerState.settledPage
}
MediaViewerPage(
isDisplayed = page == pagerState.settledPage,
isDisplayed = isDisplayed,
showOverlay = showOverlay,
bottomPaddingInPixels = bottomPaddingInPixels,
data = dataForPage,
Expand All @@ -157,7 +162,8 @@ fun MediaViewerView(
},
onShowOverlayChange = {
showOverlay = it
}
},
isUserSelected = (state.listData[page] as? MediaViewerPageData.MediaViewerData)?.eventId == state.initiallySelectedEventId,
)
// Bottom bar
AnimatedVisibility(visible = showOverlay, enter = fadeIn(), exit = fadeOut()) {
Expand Down Expand Up @@ -273,6 +279,7 @@ private fun MediaViewerPage(
bottomPaddingInPixels: Int,
data: MediaViewerPageData.MediaViewerData,
textFileViewer: TextFileViewer,
isUserSelected: Boolean,
onDismiss: () -> Unit,
onRetry: () -> Unit,
onDismissError: () -> Unit,
Expand Down Expand Up @@ -328,6 +335,7 @@ private fun MediaViewerPage(
currentOnShowOverlayChange(!currentShowOverlay)
}
},
isUserSelected = isUserSelected,
)
ThumbnailView(
mediaInfo = data.mediaInfo,
Expand Down
Loading