Skip to content

Commit

Permalink
feat: use SeekHandler component
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
Thalys Matias Carrara committed Jun 8, 2023
1 parent fd10066 commit 306da50
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package com.profusion.androidenhancedvideoplayer.components

import android.content.res.Configuration
import android.net.Uri
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.*
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
Expand All @@ -14,10 +18,13 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.*
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.*
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
Expand All @@ -27,6 +34,7 @@ import androidx.media3.ui.AspectRatioFrameLayout
import androidx.media3.ui.PlayerView
import com.profusion.androidenhancedvideoplayer.components.playerOverlay.ControlsCustomization
import com.profusion.androidenhancedvideoplayer.components.playerOverlay.PlayerControls
import com.profusion.androidenhancedvideoplayer.components.playerOverlay.SeekHandler
import com.profusion.androidenhancedvideoplayer.components.playerOverlay.SettingsControlsCustomization
import com.profusion.androidenhancedvideoplayer.utils.TimeoutEffect
import com.profusion.androidenhancedvideoplayer.utils.fillMaxSizeOnLandscape
Expand All @@ -38,6 +46,8 @@ import com.profusion.androidenhancedvideoplayer.utils.setStatusBarVisibility
private const val MAIN_PACKAGE_PATH_PREFIX = "android.resource://"
private const val CURRENT_TIME_TICK_IN_MS = 50L

private const val DEFAULT_SEEK_TIME_MS = 10 * 1000L // 10 seconds

@androidx.annotation.OptIn(UnstableApi::class)
@Composable
fun EnhancedVideoPlayer(
Expand All @@ -49,7 +59,8 @@ fun EnhancedVideoPlayer(
soundOff: Boolean = true,
currentTimeTickInMs: Long = CURRENT_TIME_TICK_IN_MS,
controlsCustomization: ControlsCustomization = ControlsCustomization(),
settingsControlsCustomization: SettingsControlsCustomization = SettingsControlsCustomization()
settingsControlsCustomization: SettingsControlsCustomization = SettingsControlsCustomization(),
transformSeekIncrementRatio: (tapCount: Int) -> Long = { it -> it * DEFAULT_SEEK_TIME_MS }
) {
val context = LocalContext.current
val mainPackagePath = "$MAIN_PACKAGE_PATH_PREFIX${context.packageName}/"
Expand All @@ -65,7 +76,8 @@ fun EnhancedVideoPlayer(
soundOff = soundOff,
currentTimeTickInMs = currentTimeTickInMs,
controlsCustomization = controlsCustomization,
settingsControlsCustomization = settingsControlsCustomization
settingsControlsCustomization = settingsControlsCustomization,
transformSeekIncrementRatio = { transformSeekIncrementRatio(it) }
)
}

Expand All @@ -80,6 +92,7 @@ fun EnhancedVideoPlayer(
soundOff: Boolean = true,
currentTimeTickInMs: Long = CURRENT_TIME_TICK_IN_MS,
controlsCustomization: ControlsCustomization = ControlsCustomization(),
transformSeekIncrementRatio: (tapCount: Int) -> Long = { it -> it * DEFAULT_SEEK_TIME_MS },
settingsControlsCustomization: SettingsControlsCustomization = SettingsControlsCustomization()
) {
val context = LocalContext.current
Expand All @@ -96,7 +109,6 @@ fun EnhancedVideoPlayer(
prepare()
}
}

var isPlaying by remember { mutableStateOf(exoPlayer.isPlaying) }
var hasEnded by remember { mutableStateOf(exoPlayer.playbackState == ExoPlayer.STATE_ENDED) }
var isControlsVisible by remember { mutableStateOf(false) }
Expand All @@ -114,6 +126,10 @@ fun EnhancedVideoPlayer(
context.setNavigationBarVisibility(shouldShowSystemUi)
}

fun setControlsVisibility(visible: Boolean) {
isControlsVisible = visible
}

DisposableEffect(context) {
val listener = object : Player.Listener {
override fun onEvents(player: Player, events: Player.Events) {
Expand Down Expand Up @@ -142,11 +158,6 @@ fun EnhancedVideoPlayer(

Box(
modifier = Modifier
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() },
onClick = { isControlsVisible = !isControlsVisible }
)
.background(Color.Black)
.fillMaxSizeOnLandscape(orientation)
.testTag("VideoPlayerParent"),
Expand All @@ -167,6 +178,18 @@ fun EnhancedVideoPlayer(
}
}
)
Box(modifier = Modifier.matchParentSize()) {
SeekHandler(
disableSeekForward = hasEnded,
isControlsVisible = isControlsVisible,
exoPlayer = exoPlayer,
controlsCustomization = controlsCustomization,
toggleControlsVisibility = { isControlsVisible = !isControlsVisible },
setControlsVisibility = ::setControlsVisibility,
transformSeekIncrementRatio = transformSeekIncrementRatio
)
}

PlayerControls(
title = title,
isVisible = isControlsVisible,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class ControlsCustomization(
val nextIconContent: @Composable () -> Unit = { NextIcon() },
val fullScreenIconContent: @Composable () -> Unit = { FullScreenIcon() },
val exitFullScreenIconContent: @Composable () -> Unit = { ExitFullScreenIcon() },
val settingsIconContent: @Composable () -> Unit = { SettingsIcon() }
val settingsIconContent: @Composable () -> Unit = { SettingsIcon() },
val forwardIconContent: @Composable (modifier: Modifier) -> Unit = { ForwardIcon(it) },
val rewindIconContent: @Composable (modifier: Modifier) -> Unit = { RewindIcon(it) }
)

@Composable
fun PlayerControls(
modifier: Modifier = Modifier,
title: String? = null,
isVisible: Boolean,
isPlaying: Boolean,
Expand All @@ -33,8 +36,7 @@ fun PlayerControls(
onSpeedSelected: (Float) -> Unit,
onSeekBarValueChange: (Long) -> Unit,
customization: ControlsCustomization,
settingsControlsCustomization: SettingsControlsCustomization,
modifier: Modifier = Modifier
settingsControlsCustomization: SettingsControlsCustomization
) {
PlayerControlsScaffold(
modifier = modifier.testTag("PlayerControlsParent"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.profusion.androidenhancedvideoplayer.styling.Colors

@Composable
fun PlayerControlsScaffold(
Expand All @@ -25,7 +25,7 @@ fun PlayerControlsScaffold(
enter = fadeIn(),
exit = fadeOut(),
modifier = modifier
.background(Color.Black.copy(alpha = 0.6f))
.background(Colors.controlsShadow)
) {
Column(
modifier = Modifier.fillMaxSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,23 @@ fun CheckIcon(modifier: Modifier = Modifier) {
modifier = modifier
)
}

@Composable
fun ForwardIcon(modifier: Modifier = Modifier) {
Icon(
painter = painterResource(id = R.drawable.ic_forward),
tint = Color.White,
contentDescription = stringResource(R.string.controls_forward_description),
modifier = modifier.testTag("ForwardIcon")
)
}

@Composable
fun RewindIcon(modifier: Modifier = Modifier) {
Icon(
painter = painterResource(id = R.drawable.ic_rewind),
tint = Color.White,
contentDescription = stringResource(R.string.controls_rewind_description),
modifier = modifier.testTag("RewindIcon")
)
}

0 comments on commit 306da50

Please sign in to comment.