-
Notifications
You must be signed in to change notification settings - Fork 589
Add MetadataState
to :ui:compose
#2700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MGaetan89
wants to merge
4
commits into
androidx:main
Choose a base branch
from
MGaetan89:add-compose-metadata-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b6a3d09
Add `MetadataState` to `:ui:compose`
MGaetan89 681b250
Check for `Player.COMMAND_GET_CURRENT_MEDIA_ITEM` before getting the …
MGaetan89 2179e5b
Format with google-java-format and add RELEAESENOTES
oceanjules 4860dcd
Replace usage of `listen` with `listenTo`
MGaetan89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
libraries/ui_compose/src/main/java/androidx/media3/ui/compose/state/MetadataState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.media3.ui.compose.state | ||
|
||
import android.net.Uri | ||
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 | ||
import androidx.compose.runtime.setValue | ||
import androidx.media3.common.Player | ||
import androidx.media3.common.listenTo | ||
import androidx.media3.common.util.UnstableApi | ||
|
||
/** | ||
* Remembers the value of a [MetadataState] created based on the passed [Player] and launches a | ||
* coroutine to listen to the [Player's][Player] changes. If the [Player] instance changes between | ||
* compositions, this produces and remembers a new [MetadataState]. | ||
*/ | ||
@UnstableApi | ||
@Composable | ||
fun rememberMetadataState(player: Player): MetadataState { | ||
val metadataState = remember(player) { MetadataState(player) } | ||
LaunchedEffect(player) { metadataState.observe() } | ||
return metadataState | ||
} | ||
|
||
/** | ||
* State that holds information to correctly deal with UI components related to the current | ||
* [MediaItem][androidx.media3.common.MediaItem] metadata. | ||
* | ||
* @property[uri] The URI of the current media item, if available. | ||
*/ | ||
@UnstableApi | ||
class MetadataState(private val player: Player) { | ||
var uri by mutableStateOf(player.getMediaItemUriWithCommandCheck()) | ||
private set | ||
|
||
suspend fun observe(): Nothing { | ||
player.listenTo(Player.EVENT_AVAILABLE_COMMANDS_CHANGED, Player.EVENT_MEDIA_ITEM_TRANSITION) { | ||
uri = getMediaItemUriWithCommandCheck() | ||
} | ||
} | ||
|
||
private fun Player.getMediaItemUriWithCommandCheck(): Uri? { | ||
return if (isCommandAvailable(Player.COMMAND_GET_CURRENT_MEDIA_ITEM)) { | ||
currentMediaItem?.localConfiguration?.uri | ||
} else { | ||
null | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
libraries/ui_compose/src/test/java/androidx/media3/ui/compose/state/MetadataStateTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.media3.ui.compose.state | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.core.net.toUri | ||
import androidx.media3.common.MediaItem | ||
import androidx.media3.common.Player | ||
import androidx.media3.common.SimpleBasePlayer.MediaItemData | ||
import androidx.media3.ui.compose.utils.TestPlayer | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
/** Unit test for [MetadataState]. */ | ||
@RunWith(AndroidJUnit4::class) | ||
class MetadataStateTest { | ||
|
||
@get:Rule val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun uri_emptyPlaylist_returnsNull() { | ||
val player = TestPlayer(playbackState = Player.STATE_IDLE, playlist = emptyList()) | ||
|
||
lateinit var state: MetadataState | ||
composeTestRule.setContent { state = rememberMetadataState(player) } | ||
|
||
assertThat(state.uri).isNull() | ||
} | ||
|
||
@Test | ||
fun uri_singleItemWithoutUri_returnsNull() { | ||
val player = TestPlayer(playlist = listOf(MediaItemData.Builder("uid_1").build())) | ||
|
||
lateinit var state: MetadataState | ||
composeTestRule.setContent { state = rememberMetadataState(player) } | ||
|
||
assertThat(state.uri).isNull() | ||
} | ||
|
||
@Test | ||
fun uri_singleItemWithUri_returnsTheUri() { | ||
val uri = "https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd".toUri() | ||
val player = | ||
TestPlayer( | ||
playlist = | ||
listOf(MediaItemData.Builder("uid_1").setMediaItem(MediaItem.fromUri(uri)).build()) | ||
) | ||
|
||
lateinit var state: MetadataState | ||
composeTestRule.setContent { state = rememberMetadataState(player) } | ||
|
||
assertThat(state.uri).isEqualTo(uri) | ||
} | ||
|
||
@Test | ||
fun uri_transitionBetweenItems_returnsUpdatedUri() { | ||
val uri1 = "https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd".toUri() | ||
val uri2 = | ||
"https://storage.googleapis.com/exoplayer-test-media-1/mp4/dizzy-with-tx3g.mp4".toUri() | ||
val player = | ||
TestPlayer( | ||
playlist = | ||
listOf( | ||
MediaItemData.Builder("uid_1").setMediaItem(MediaItem.fromUri(uri1)).build(), | ||
MediaItemData.Builder("uid_2").build(), | ||
MediaItemData.Builder("uid_3").setMediaItem(MediaItem.fromUri(uri2)).build(), | ||
) | ||
) | ||
|
||
lateinit var state: MetadataState | ||
composeTestRule.setContent { state = rememberMetadataState(player) } | ||
|
||
assertThat(state.uri).isEqualTo(uri1) | ||
|
||
player.seekToNext() | ||
composeTestRule.waitForIdle() | ||
|
||
assertThat(state.uri).isNull() | ||
|
||
player.seekToNext() | ||
composeTestRule.waitForIdle() | ||
|
||
assertThat(state.uri).isEqualTo(uri2) | ||
} | ||
|
||
@Test | ||
fun uri_getCurrentMediaItemCommandBecomesAvailable_returnsUpdatedUri() { | ||
val uri = "https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd".toUri() | ||
val player = | ||
TestPlayer( | ||
playlist = | ||
listOf(MediaItemData.Builder("uid_1").setMediaItem(MediaItem.fromUri(uri)).build()) | ||
) | ||
player.removeCommands(Player.COMMAND_GET_CURRENT_MEDIA_ITEM) | ||
|
||
lateinit var state: MetadataState | ||
composeTestRule.setContent { state = rememberMetadataState(player) } | ||
|
||
assertThat(state.uri).isNull() | ||
|
||
player.addCommands(Player.COMMAND_GET_CURRENT_MEDIA_ITEM) | ||
composeTestRule.waitForIdle() | ||
|
||
assertThat(state.uri).isEqualTo(uri) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.