Skip to content

Commit

Permalink
fix(metadata): fix updating metadata (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz authored Aug 10, 2023
1 parent 2a2526f commit 9f202ec
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 180 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ jobs:
with:
distribution: 'adopt'
java-version: '11'
- name: Build Sample App
run: ./gradlew :kotlin-audio-sample:assembleDebug
- name: Build Example App
run: ./gradlew :kotlin-audio-example:assembleDebug
6 changes: 3 additions & 3 deletions kotlin-audio-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ dependencies {
}

implementation(project(":kotlin-audio"))
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
implementation("androidx.activity:activity-compose:1.7.2")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material3:material3:1.1.1")
implementation("androidx.compose.material:material-icons-extended")
implementation("io.coil-kt:coil-compose:2.4.0")
testImplementation("junit:junit:4.13.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
Expand All @@ -34,6 +38,7 @@ import com.doublesymmetry.kotlinaudio.models.NotificationConfig
import com.doublesymmetry.kotlinaudio.models.RepeatMode
import com.doublesymmetry.kotlinaudio.models.PlayerConfig
import com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer
import com.example.kotlin_audio_example.ui.component.ActionBottomSheet
import com.example.kotlin_audio_example.ui.component.PlayerControls
import com.example.kotlin_audio_example.ui.component.TrackDisplay
import com.example.kotlin_audio_example.ui.theme.KotlinAudioTheme
Expand All @@ -47,7 +52,9 @@ import kotlin.time.Duration.Companion.seconds
class MainActivity : ComponentActivity() {
private lateinit var player: QueuedAudioPlayer

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
Timber.plant(Timber.DebugTree())
super.onCreate(savedInstanceState)

player = QueuedAudioPlayer(
Expand All @@ -72,27 +79,46 @@ class MainActivity : ComponentActivity() {
var duration by remember { mutableStateOf(0L) }
var isLive by remember { mutableStateOf(false) }

Inner(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onPrevious = { player.previous() },
onNext = { player.next() },
isPaused = state.value != AudioPlayerState.PLAYING,
onPlayPause = {
if (player.playerState == AudioPlayerState.PLAYING) {
player.pause()
} else {
player.play()
var showSheet by remember { mutableStateOf(false) }

if (showSheet) {
ActionBottomSheet(
onDismiss = { showSheet = false },
onRandomMetadata = {
val currentIndex = player.currentIndex
val track = tracks[currentIndex].copy(
title = "Random Title - ${System.currentTimeMillis()}",
artwork = "https://random.imagecdn.app/800/800"
)
player.replaceItem(currentIndex, track)
}
},
onSeek = { player.seek(it, TimeUnit.MILLISECONDS) }
)
)
}

LaunchedEffect(key1 = player, key2 = player.event.audioItemTransition) {
KotlinAudioTheme {
MainScreen(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onPrevious = { player.previous() },
onNext = { player.next() },
isPaused = state.value != AudioPlayerState.PLAYING,
onTopBarAction = { showSheet = true },
onPlayPause = {
if (player.playerState == AudioPlayerState.PLAYING) {
player.pause()
} else {
player.play()
}
},
onSeek = { player.seek(it, TimeUnit.MILLISECONDS) }
)
}

LaunchedEffect(key1 = player, key2 = player.event.audioItemTransition, key3 = player.event.onPlayerActionTriggeredExternally) {
player.event.audioItemTransition
.onEach {
title = player.currentItem?.title ?: ""
Expand Down Expand Up @@ -121,15 +147,13 @@ class MainActivity : ComponentActivity() {
.launchIn(this)
}

if (player.playerState == AudioPlayerState.PLAYING) {
LaunchedEffect(Unit) {
while(true) {
position = player.position
duration = player.duration
isLive = player.isCurrentMediaItemLive
LaunchedEffect(Unit) {
while(true) {
position = player.position
duration = player.duration
isLive = player.isCurrentMediaItemLive

delay(1.seconds / 30)
}
delay(1.seconds / 30)
}
}
}
Expand Down Expand Up @@ -200,7 +224,7 @@ class MainActivity : ComponentActivity() {

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Inner(
fun MainScreen(
title: String,
artist: String,
artwork: String,
Expand All @@ -210,55 +234,61 @@ fun Inner(
onPrevious: () -> Unit = {},
onNext: () -> Unit = {},
isPaused: Boolean,
onTopBarAction: () -> Unit = {},
onPlayPause: () -> Unit = {},
onSeek: (Long) -> Unit = {},
) {
KotlinAudioTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
modifier = Modifier
.fillMaxSize()
) {
TopAppBar(
title = {
Text(
text = "Kotlin Audio Example",
color = MaterialTheme.colorScheme.onPrimary
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(modifier = Modifier.fillMaxSize()) {
TopAppBar(
title = {
Text(
text = "Kotlin Audio Example",
color = MaterialTheme.colorScheme.onPrimary
)
},
actions = {
IconButton(onClick = onTopBarAction) {
Icon(
Icons.Default.MoreVert,
contentDescription = "Settings",
tint = MaterialTheme.colorScheme.onPrimary
)
},
colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = MaterialTheme.colorScheme.primary)
)
TrackDisplay(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onSeek = onSeek,
)
Spacer(modifier = Modifier.weight(1f))
PlayerControls(
onPrevious = onPrevious,
onNext = onNext,
isPaused = isPaused,
onPlayPause = onPlayPause,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 60.dp))
}
}
},
colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = MaterialTheme.colorScheme.primary)
)
TrackDisplay(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onSeek = onSeek,
modifier = Modifier.padding(top = 46.dp)
)
Spacer(modifier = Modifier.weight(1f))
PlayerControls(
onPrevious = onPrevious,
onNext = onNext,
isPaused = isPaused,
onPlayPause = onPlayPause,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 60.dp)
)
}
}
}
@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
@Composable
fun ContentPreview() {
KotlinAudioTheme {
Inner(
MainScreen(
title = "Title",
artist = "Artist",
artwork = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.kotlin_audio_example.ui.component

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
@ExperimentalMaterial3Api
fun ActionBottomSheet(
onDismiss: () -> Unit,
onRandomMetadata: () -> Unit,
) {
val modalBottomSheetState = rememberModalBottomSheetState()

ModalBottomSheet(
onDismissRequest = { onDismiss() },
sheetState = modalBottomSheetState,
dragHandle = { BottomSheetDefaults.DragHandle() },
) {
InnerSheet(onRandomMetadata = onRandomMetadata)
}
}

@Composable
fun InnerSheet(onRandomMetadata: () -> Unit = {}) {
// Add a button to perform an action when clicked
Button(
onClick = onRandomMetadata,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
) {
Text("Metadata: Update Title Randomly")
}
}

@Preview
@ExperimentalMaterial3Api
@Composable
fun ActionBottomSheetPreview() {
InnerSheet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ fun TrackDisplay(
position: Long,
duration: Long,
isLive: Boolean,
modifier: Modifier = Modifier,
onSeek: (Long) -> Unit = {},
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = modifier) {
if (artwork.isEmpty())
Image(
painter = painterResource(id = R.drawable.ic_launcher_background),
contentDescription = "Album Cover",
modifier = Modifier
.fillMaxWidth()
.height(240.dp)
.padding(top = 48.dp)
.height(200.dp)
)
else
AsyncImage(
Expand All @@ -63,7 +63,7 @@ fun TrackDisplay(
Text(
text = "Live",
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.padding(top = 16.dp, bottom = 16.dp)
modifier = Modifier.padding(top = 16.dp)
)
else
Column {
Expand All @@ -83,7 +83,7 @@ fun TrackDisplay(
Row(
modifier = Modifier
.fillMaxWidth()
.padding(top = 0.dp, start = 16.dp, end = 16.dp, bottom = 16.dp),
.padding(start = 16.dp, end = 16.dp),
) {
Text(
text = position.millisecondsToString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data class DefaultAudioItem(
override var artist: String? = null,
override var title: String? = null,
override var albumTitle: String? = null,
override val artwork: String? = null,
override var artwork: String? = null,
override val duration: Long = -1,
override val options: AudioItemOptions? = null,
) : AudioItem
Expand Down

This file was deleted.

Loading

0 comments on commit 9f202ec

Please sign in to comment.