Skip to content

Commit

Permalink
feat/WIP: Improved a little bit the implementation of the media player
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <gabilessto@gmail.com>
  • Loading branch information
BobbyESP committed Apr 17, 2024
1 parent ee34999 commit 0597b16
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,22 @@ class MediaServiceHandler @Inject constructor(
suspend fun onPlayerEvent(playerEvent: PlayerEvent) {
when (playerEvent) {
is PlayerEvent.PlayPause -> {
if (player.isPlaying) {
player.pause()
_mediaState.update {
MediaState.Playing(false)
when (player.isPlaying) {
true -> {
player.pause()
_mediaState.update {
MediaState.Playing(false)
}
stopProgressUpdate()
}
stopProgressUpdate()
} else {
player.play()
_mediaState.update {
MediaState.Playing(true)

false -> {
player.play()
_mediaState.update {
MediaState.Playing(true)
}
startProgressUpdate()
}
startProgressUpdate()
}
}

Expand Down Expand Up @@ -212,7 +216,7 @@ sealed class PlayerEvent {
data class UpdateProgress(val updatedProgress: Long) : PlayerEvent()
}

sealed class MediaState {
sealed class MediaState { //TODO: NOT USE SEALED CLASSES
data object Idle : MediaState()
data class Ready(val duration: Long) : MediaState()
data class Progress(val progress: Long) : MediaState()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bobbyesp.mediaplayer.service

import android.content.Intent
import android.os.Binder
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.session.MediaSession
Expand Down Expand Up @@ -43,4 +44,9 @@ class MediaplayerService : MediaSessionService() {
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession {
return mediaSession
}

inner class MusicBinder : Binder() {
val service: MediaplayerService
get() = this@MediaplayerService
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/bobbyesp/metadator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MainActivity : ComponentActivity() {
}
activity = this
setupFirebase()
startMediaPlayerService()
setContent {
val windowSizeClass = calculateWindowSizeClass(this)
AppLocalSettingsProvider(windowSizeClass.widthSizeClass) {
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/com/bobbyesp/metadator/presentation/Navigation.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.bobbyesp.metadator.presentation

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand All @@ -18,7 +17,6 @@ import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -46,12 +44,15 @@ import com.bobbyesp.metadator.presentation.common.NavArgs
import com.bobbyesp.metadator.presentation.common.Route
import com.bobbyesp.metadator.presentation.common.TagEditorParcelableSongParamType
import com.bobbyesp.metadator.presentation.common.routesToNavigate
import com.bobbyesp.metadator.presentation.components.others.CollapsedPlayerHeight
import com.bobbyesp.metadator.presentation.components.others.PlayerAnimationSpec
import com.bobbyesp.metadator.presentation.pages.MediaStorePageViewModel
import com.bobbyesp.metadator.presentation.pages.home.HomePage
import com.bobbyesp.metadator.presentation.pages.mediaplayer.MediaplayerPage
import com.bobbyesp.metadator.presentation.pages.mediaplayer.MediaplayerViewModel
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.ID3MetadataEditorPage
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.ID3MetadataEditorPageViewModel
import com.bobbyesp.ui.components.bottomsheet.draggable.rememberDraggableBottomSheetState
import com.bobbyesp.ui.motion.animatedComposable
import com.bobbyesp.ui.motion.slideInVerticallyComposable
import com.bobbyesp.utilities.navigation.getParcelable
Expand Down Expand Up @@ -79,26 +80,26 @@ fun Navigator() {

val scope = rememberCoroutineScope()

val showSnackbarMessage: suspend (String) -> Unit = { message ->
snackbarHostState.showSnackbar(message)
}

//able to open drawer when the user is in one of the main routes (root routes)
val canOpenDrawer by remember(currentRoute) {
mutableStateOf(routesToNavigate.fastAny { it.route == currentRootRoute.value })
}

val mediaStoreViewModel = hiltViewModel<MediaStorePageViewModel>()
val mediaplayerViewModel = hiltViewModel<MediaplayerViewModel>()

LaunchedEffect(canOpenDrawer) {
Log.i("Navigator", "canOpenDrawer: $canOpenDrawer")
}

Box(
BoxWithConstraints(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
) {
val mediaPlayerSheetState = rememberDraggableBottomSheetState(
dismissedBound = 0.dp,
collapsedBound = CollapsedPlayerHeight,
expandedBound = maxHeight,
animationSpec = PlayerAnimationSpec,
)

ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = canOpenDrawer,
Expand Down Expand Up @@ -177,8 +178,7 @@ fun Navigator() {
route = Route.MediaplayerNavigator.route
) {
animatedComposable(Route.MediaplayerNavigator.Mediaplayer.route) {
val viewModel = hiltViewModel<MediaplayerViewModel>()
MediaplayerPage(viewModel)
MediaplayerPage(mediaplayerViewModel, mediaPlayerSheetState)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import androidx.compose.animation.core.spring
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

val CollapsedPlayerHeight = 64.dp
val CollapsedPlayerHeight = 84.dp

val PlayerAnimationSpec = spring<Dp>(stiffness = Spring.StiffnessMediumLow)
Loading

0 comments on commit 0597b16

Please sign in to comment.