Skip to content

Commit

Permalink
feat: use VolumeControl composable
Browse files Browse the repository at this point in the history
Closes #52
  • Loading branch information
Thalys Matias Carrara authored and thalysmcarrara committed Jul 5, 2023
1 parent 09b5ea8 commit 6aab32d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.profusion.androidenhancedvideoplayer.utils.TimeoutEffect
import com.profusion.androidenhancedvideoplayer.utils.TrackQualityAuto
import com.profusion.androidenhancedvideoplayer.utils.TrackQualityItemListSaver
import com.profusion.androidenhancedvideoplayer.utils.TrackQualityItemSaver
import com.profusion.androidenhancedvideoplayer.utils.VolumeController
import com.profusion.androidenhancedvideoplayer.utils.fillMaxSizeOnLandscape
import com.profusion.androidenhancedvideoplayer.utils.generateTrackQualityOptions
import com.profusion.androidenhancedvideoplayer.utils.getDeviceRealSizeDp
Expand Down Expand Up @@ -81,9 +82,9 @@ fun EnhancedVideoPlayer(
settingsControlsCustomization: SettingsControlsCustomization = SettingsControlsCustomization()
) {
val context = LocalContext.current

val configuration = LocalConfiguration.current
val orientation = configuration.orientation
val volumeController = remember { VolumeController(context) }

var isPlaying by remember { mutableStateOf(exoPlayer.isPlaying) }
var isBuffering by remember {
Expand All @@ -104,8 +105,11 @@ fun EnhancedVideoPlayer(
}
var currentOffsetXPreview by remember { mutableStateOf(INITIAL_PREVIEW_OFFSET) }

var deviceVolume by remember { mutableStateOf(volumeController.getDeviceVolume()) }
val brightnessMutableInteractionSource = remember { MutableInteractionSource() }
val isBrightnessSliderDragged by brightnessMutableInteractionSource.collectIsDraggedAsState()
val volumeMutableInteractionSource = remember { MutableInteractionSource() }
val isVolumeSliderDragged by volumeMutableInteractionSource.collectIsDraggedAsState()
val timeBarMutableInteractionSource = remember { MutableInteractionSource() }
val isTimeBarDragged by timeBarMutableInteractionSource.collectIsDraggedAsState()

Expand Down Expand Up @@ -142,6 +146,7 @@ fun EnhancedVideoPlayer(
currentTime = player.contentPosition
totalDuration = player.duration
loop = player.repeatMode == ExoPlayer.REPEAT_MODE_ALL
deviceVolume = player.deviceVolume
super.onEvents(player, events)
}

Expand Down Expand Up @@ -188,13 +193,20 @@ fun EnhancedVideoPlayer(
}
}

LaunchedEffect(isControlsVisible, isPlaying, isBrightnessSliderDragged, isTimeBarDragged) {
LaunchedEffect(
isControlsVisible,
isPlaying,
isBrightnessSliderDragged,
isVolumeSliderDragged,
isTimeBarDragged
) {
if (
isControlsVisible &&
isPlaying &&
controlsVisibilityDurationInMs > 0 &&
!isBrightnessSliderDragged &&
!isTimeBarDragged
!isTimeBarDragged &&
!isVolumeSliderDragged
) {
delay(controlsVisibilityDurationInMs)
isControlsVisible = false
Expand Down Expand Up @@ -268,8 +280,10 @@ fun EnhancedVideoPlayer(
isFullScreen = isFullScreen,
isBrightnessSliderDragged = isBrightnessSliderDragged,
isTimeBarDragged = isTimeBarDragged,
isVolumeSliderDragged = isVolumeSliderDragged,
hasEnded = hasEnded,
brightnessMutableInteractionSource = brightnessMutableInteractionSource,
volumeMutableInteractionSource = volumeMutableInteractionSource,
timeBarMutableInteractionSource = timeBarMutableInteractionSource,
totalDuration = totalDuration,
currentTime = { currentTime },
Expand Down Expand Up @@ -298,6 +312,10 @@ fun EnhancedVideoPlayer(
currentImagePreview = previewThumbnailBuilder(it)
}
},
deviceVolume = { deviceVolume },
maxVolumeValue = { volumeController.getMaxVolumeValue() },
setDeviceVolume = { volumeController.setDeviceVolume(it) },
shouldShowVolumeControl = !volumeController.deviceHasVolumeFixedPolicy,
customization = controlsCustomization
)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,38 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import com.profusion.androidenhancedvideoplayer.components.playerOverlay.sideControls.BrightnessControl
import com.profusion.androidenhancedvideoplayer.components.playerOverlay.sideControls.VolumeControl

@Composable
fun MiddleControls(
modifier: Modifier = Modifier,
shouldShowContent: Boolean = true,
shouldShowVolumeControl: Boolean,
isPlaying: Boolean,
isBuffering: Boolean,
isFullScreen: Boolean,
isTimeBarDragged: Boolean,
hasEnded: Boolean,
customization: ControlsCustomization,
brightnessMutableInteractionSource: MutableInteractionSource,
volumeMutableInteractionSource: MutableInteractionSource,
onPreviousClick: () -> Unit,
onNextClick: () -> Unit,
onPauseToggle: () -> Unit
onPauseToggle: () -> Unit,
deviceVolume: () -> Int,
maxVolumeValue: () -> Int,
setDeviceVolume: (Int) -> Unit
) {
Box(
modifier = modifier
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
if (!isTimeBarDragged) {
val shouldShowSideControls = !isTimeBarDragged && isFullScreen
if (shouldShowSideControls) {
BrightnessControl(
modifier = Modifier.align(Alignment.CenterStart),
isFullScreen = isFullScreen,
customization = customization,
brightnessMutableInteractionSource = brightnessMutableInteractionSource
)
Expand Down Expand Up @@ -66,5 +73,15 @@ fun MiddleControls(
}
}
}
if (shouldShowVolumeControl && shouldShowSideControls) {
VolumeControl(
modifier = Modifier.align(Alignment.CenterEnd),
volume = deviceVolume,
maxVolumeValue = maxVolumeValue,
setDeviceVolume = setDeviceVolume,
customization = customization,
volumeMutableInteractionSource = volumeMutableInteractionSource
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ data class ControlsCustomization(
val brightnessHighIconContent: @Composable () -> Unit = { BrightnessHighIcon() },
val brightnessMedIconContent: @Composable () -> Unit = { BrightnessMedIcon() },
val forwardIconContent: @Composable (modifier: Modifier) -> Unit = { ForwardIcon(it) },
val rewindIconContent: @Composable (modifier: Modifier) -> Unit = { RewindIcon(it) }
val rewindIconContent: @Composable (modifier: Modifier) -> Unit = { RewindIcon(it) },
val volumeMedIconContent: @Composable () -> Unit = { VolumeMedIcon() },
val volumeHighIconContent: @Composable () -> Unit = { VolumeHighIcon() },
val volumeOffIconContent: @Composable () -> Unit = { VolumeOffIcon() }
)

@Composable
Expand All @@ -36,9 +39,14 @@ fun PlayerControls(
isFullScreen: Boolean,
isBrightnessSliderDragged: Boolean,
isTimeBarDragged: Boolean,
isVolumeSliderDragged: Boolean,
hasEnded: Boolean,
shouldShowVolumeControl: Boolean,
brightnessMutableInteractionSource: MutableInteractionSource,
volumeMutableInteractionSource: MutableInteractionSource,
timeBarMutableInteractionSource: MutableInteractionSource,
deviceVolume: () -> Int,
maxVolumeValue: () -> Int,
currentTime: () -> Long,
bufferedPosition: () -> Long,
totalDuration: Long,
Expand All @@ -49,8 +57,10 @@ fun PlayerControls(
onSettingsToggle: () -> Unit,
onSeekBarValueFinished: (Long) -> Unit,
onSeekBarValueChange: (Long) -> Unit,
setDeviceVolume: (Int) -> Unit,
customization: ControlsCustomization
) {
val shouldShowContent = !isBrightnessSliderDragged && !isVolumeSliderDragged
PlayerControlsScaffold(
modifier = modifier.testTag("PlayerControlsParent"),
isVisible = isVisible,
Expand All @@ -60,13 +70,13 @@ fun PlayerControls(
TopControls(
modifier = it,
title = title,
shouldShowContent = !isBrightnessSliderDragged
shouldShowContent = shouldShowContent
)
},
bottomContent = {
BottomControls(
modifier = it,
shouldShowContent = !isBrightnessSliderDragged,
shouldShowContent = shouldShowContent,
isFullScreen = isFullScreen,
currentTime = currentTime,
bufferedPosition = bufferedPosition,
Expand All @@ -82,17 +92,22 @@ fun PlayerControls(
) {
MiddleControls(
modifier = it,
shouldShowContent = !isBrightnessSliderDragged && !isTimeBarDragged,
shouldShowContent = shouldShowContent && !isTimeBarDragged,
isTimeBarDragged = isTimeBarDragged,
shouldShowVolumeControl = shouldShowVolumeControl,
isPlaying = isPlaying,
isBuffering = isBuffering,
isFullScreen = isFullScreen,
hasEnded = hasEnded,
customization = customization,
brightnessMutableInteractionSource = brightnessMutableInteractionSource,
volumeMutableInteractionSource = volumeMutableInteractionSource,
onPreviousClick = onPreviousClick,
onNextClick = onNextClick,
onPauseToggle = onPauseToggle
onPauseToggle = onPauseToggle,
deviceVolume = deviceVolume,
maxVolumeValue = maxVolumeValue,
setDeviceVolume = setDeviceVolume
)
}
}
Expand All @@ -102,6 +117,7 @@ fun PlayerControls(
private fun PreviewPlayerControls() {
PlayerControls(
title = "Really long title that should be truncated",
isBuffering = false,
isVisible = true,
isPlaying = true,
hasEnded = false,
Expand All @@ -111,16 +127,21 @@ private fun PreviewPlayerControls() {
currentTime = { 0L },
bufferedPosition = { 50L },
totalDuration = 100L,
brightnessMutableInteractionSource = MutableInteractionSource(),
isVolumeSliderDragged = false,
deviceVolume = { 4 },
onPreviousClick = {},
onPauseToggle = {},
onNextClick = {},
onFullScreenToggle = {},
onSettingsToggle = {},
onSeekBarValueFinished = {},
onSeekBarValueChange = {},
maxVolumeValue = { 15 },
setDeviceVolume = {},
shouldShowVolumeControl = true,
customization = ControlsCustomization(),
timeBarMutableInteractionSource = MutableInteractionSource(),
isBuffering = false
volumeMutableInteractionSource = MutableInteractionSource(),
brightnessMutableInteractionSource = MutableInteractionSource()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,33 @@ fun BrightnessMedIcon(modifier: Modifier = Modifier) {
modifier = modifier.testTag("BrightnessMedIcon")
)
}

@Composable
fun VolumeMedIcon() {
Icon(
painter = painterResource(id = R.drawable.ic_volume_med),
tint = Color.White,
contentDescription = stringResource(R.string.controls_volume_med),
modifier = Modifier.testTag("VolumeMedIcon")
)
}

@Composable
fun VolumeHighIcon() {
Icon(
painter = painterResource(id = R.drawable.ic_volume_high),
tint = Color.White,
contentDescription = stringResource(R.string.controls_volume_high),
modifier = Modifier.testTag("VolumeHighIcon")
)
}

@Composable
fun VolumeOffIcon() {
Icon(
painter = painterResource(id = R.drawable.ic_volume_off),
tint = Color.White,
contentDescription = stringResource(R.string.controls_volume_off),
modifier = Modifier.testTag("VolumeOffIcon")
)
}
Loading

0 comments on commit 6aab32d

Please sign in to comment.