-
Notifications
You must be signed in to change notification settings - Fork 232
Add video autoplay to media gallery #4499
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
Changes from 2 commits
33eafa1
aa3ac45
8d93877
96988e8
3f9ffb7
4155990
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
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 | ||
|
@@ -61,6 +62,7 @@ | |
localMediaViewState: LocalMediaViewState, | ||
bottomPaddingInPixels: Int, | ||
localMedia: LocalMedia?, | ||
autoplay: Boolean, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val exoPlayer = rememberExoPlayer() | ||
|
@@ -70,6 +72,7 @@ | |
bottomPaddingInPixels = bottomPaddingInPixels, | ||
exoPlayer = exoPlayer, | ||
localMedia = localMedia, | ||
autoplay = autoplay, | ||
modifier = modifier, | ||
) | ||
} | ||
|
@@ -82,13 +85,15 @@ | |
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, | ||
|
@@ -135,6 +140,12 @@ | |
} | ||
} | ||
} | ||
|
||
override fun onPlaybackStateChanged(playbackState: Int) { | ||
mediaPlayerControllerState = mediaPlayerControllerState.copy( | ||
Check warning on line 145 in libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/local/video/MediaVideoView.kt
|
||
isReady = playbackState == STATE_READY, | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -164,10 +175,16 @@ | |
) | ||
} | ||
} | ||
LaunchedEffect(isDisplayed) { | ||
// If not displayed, make sure to pause the video | ||
if (!isDisplayed) { | ||
|
||
LaunchedEffect(autoplay, isDisplayed, mediaPlayerControllerState.isReady) { | ||
val isReadyAndPlaying = mediaPlayerControllerState.isReady && mediaPlayerControllerState.isPlaying | ||
if (autoplay && isDisplayed && isReadyAndPlaying) { | ||
// When displayed, start autoplaying from the beginning | ||
exoPlayer.play() | ||
Check warning on line 183 in libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/local/video/MediaVideoView.kt
|
||
} else if (!isDisplayed && mediaPlayerControllerState.isPlaying) { | ||
// If not displayed, make sure to pause the video | ||
exoPlayer.pause() | ||
exoPlayer.seekTo(0) | ||
Check warning on line 187 in libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/local/video/MediaVideoView.kt
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why resetting to player position here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was done so the video would restart from the beginning when coming back to it, but since we don't do that anymore I think we can remove it now. Thanks for noticing it! |
||
} | ||
} | ||
if (localMedia?.uri != null) { | ||
|
@@ -259,5 +276,6 @@ | |
bottomPaddingInPixels = 0, | ||
localMediaViewState = rememberLocalMediaViewState(), | ||
localMedia = null, | ||
autoplay = false, | ||
) | ||
} |
There was a problem hiding this comment.
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.