Skip to content

Commit

Permalink
chore(example): Fix seek
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Aug 8, 2023
1 parent 9c406ea commit 2a2526f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.doublesymmetry.kotlinaudio.models.AudioPlayerState
import com.doublesymmetry.kotlinaudio.models.DefaultAudioItem
import com.doublesymmetry.kotlinaudio.models.MediaSessionCallback
import com.doublesymmetry.kotlinaudio.models.MediaType
import com.doublesymmetry.kotlinaudio.models.NotificationButton
import com.doublesymmetry.kotlinaudio.models.NotificationConfig
import com.doublesymmetry.kotlinaudio.models.RepeatMode
import com.doublesymmetry.kotlinaudio.models.PlayerConfig
import com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer
Expand All @@ -37,6 +40,8 @@ import com.example.kotlin_audio_example.ui.theme.KotlinAudioTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.seconds

class MainActivity : ComponentActivity() {
Expand All @@ -56,6 +61,8 @@ class MainActivity : ComponentActivity() {
player.playerOptions.repeatMode = RepeatMode.ALL
player.play()

setupNotification()

setContent {
val state = player.event.stateChange.collectAsState(initial = AudioPlayerState.IDLE)
var title by remember { mutableStateOf("") }
Expand All @@ -81,7 +88,8 @@ class MainActivity : ComponentActivity() {
} else {
player.play()
}
}
},
onSeek = { player.seek(it, TimeUnit.MILLISECONDS) }
)

LaunchedEffect(key1 = player, key2 = player.event.audioItemTransition) {
Expand All @@ -94,6 +102,23 @@ class MainActivity : ComponentActivity() {
isLive = player.isCurrentMediaItemLive
}
.launchIn(this)

player.event.onPlayerActionTriggeredExternally
.onEach {
when (it) {
MediaSessionCallback.PLAY -> player.play()
MediaSessionCallback.PAUSE -> player.pause()
MediaSessionCallback.NEXT -> player.next()
MediaSessionCallback.PREVIOUS -> player.previous()
MediaSessionCallback.STOP -> player.stop()
is MediaSessionCallback.SEEK -> player.seek(
it.positionMs,
TimeUnit.MILLISECONDS
)
else -> Timber.d("Event not handled")
}
}
.launchIn(this)
}

if (player.playerState == AudioPlayerState.PLAYING) {
Expand All @@ -110,6 +135,18 @@ class MainActivity : ComponentActivity() {
}
}

private fun setupNotification() {
val notificationConfig = NotificationConfig(
listOf(
NotificationButton.PLAY_PAUSE(),
NotificationButton.NEXT(isCompact = true),
NotificationButton.PREVIOUS(isCompact = true),
NotificationButton.SEEK_TO
), accentColor = null, smallIcon = null, pendingIntent = null
)
player.notificationManager.createNotification(notificationConfig)
}

companion object {
val tracks = listOf(
DefaultAudioItem(
Expand Down Expand Up @@ -174,6 +211,7 @@ fun Inner(
onNext: () -> Unit = {},
isPaused: Boolean,
onPlayPause: () -> Unit = {},
onSeek: (Long) -> Unit = {},
) {
KotlinAudioTheme {
// A surface container using the 'background' color from the theme
Expand Down Expand Up @@ -201,6 +239,7 @@ fun Inner(
position = position,
duration = duration,
isLive = isLive,
onSeek = onSeek,
)
Spacer(modifier = Modifier.weight(1f))
PlayerControls(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fun TrackDisplay(
position: Long,
duration: Long,
isLive: Boolean,
onSeek: (Long) -> Unit = {},
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
if (artwork.isEmpty())
Expand Down Expand Up @@ -72,7 +73,9 @@ fun TrackDisplay(
} else {
position.toFloat() / duration.toFloat()
},
onValueChange = { /*TODO*/ },
onValueChange = {
onSeek((it * duration).toLong())
},
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp, start = 8.dp, end = 8.dp)
Expand Down

0 comments on commit 2a2526f

Please sign in to comment.