Skip to content

Commit

Permalink
Update to AndroidX media3 1.2.0 (#364)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaëtan Muller <[email protected]>
  • Loading branch information
StaehliJ and MGaetan89 authored Dec 14, 2023
1 parent 2a7d91c commit e210118
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 25 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mockk = "1.12.5"
junit = "4.13.2"
androidJunit = "1.1.5"
espresso = "3.5.1"
media3 = "1.1.1"
media = "1.6.0"
media3 = "1.2.0"
media = "1.7.0"
guava = "31.1-android"
navigation = "2.7.4"
paging = "3.2.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object NavigationRoutes {
const val playerSwap = "player_swap"
const val exoPlayerSample = "exoplayer_sample"
const val trackingSample = "tracking_sample"
const val updatableSample = "updatable_sample"

const val homeLists = "home_lists"
const val contentLists = "content_lists"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fun ShowCaseList(navController: NavController) {
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(horizontal = MaterialTheme.paddings.baseline)
.padding(bottom = MaterialTheme.paddings.baseline)
) {
DemoListHeaderView(
title = stringResource(R.string.layouts),
Expand Down Expand Up @@ -98,11 +99,21 @@ fun ShowCaseList(navController: NavController) {
}

DemoListHeaderView(
title = stringResource(R.string.system_integration),
title = stringResource(R.string.integrations),
modifier = titleModifier
)

DemoListSectionView {
DemoListSectionView {
DemoListItemView(
title = stringResource(R.string.exoplayer_view),
modifier = itemModifier,
onClick = { navController.navigate(NavigationRoutes.exoPlayerSample) }
)
}

Divider()

DemoListItemView(
title = stringResource(R.string.auto),
modifier = itemModifier,
Expand All @@ -114,7 +125,7 @@ fun ShowCaseList(navController: NavController) {
}

DemoListHeaderView(
title = stringResource(R.string.embeddings),
title = stringResource(R.string.misc),
modifier = titleModifier
)

Expand All @@ -132,31 +143,24 @@ fun ShowCaseList(navController: NavController) {
modifier = itemModifier,
onClick = { navController.navigate(NavigationRoutes.playerSwap) }
)
}

DemoListHeaderView(
title = stringResource(R.string.exoplayer),
modifier = titleModifier
)
Divider()

DemoListSectionView {
DemoListItemView(
title = stringResource(R.string.exoplayer_view),
title = stringResource(R.string.tracker_example),
modifier = itemModifier,
onClick = { navController.navigate(NavigationRoutes.exoPlayerSample) }
onClick = { navController.navigate(NavigationRoutes.trackingSample) }
)
}

DemoListHeaderView(
title = stringResource(R.string.tracking),
modifier = titleModifier
)
Divider()

DemoListSectionView(modifier = Modifier.padding(bottom = MaterialTheme.paddings.baseline)) {
DemoListItemView(
title = stringResource(R.string.tracker_example),
title = stringResource(R.string.update_media_item_example),
modifier = itemModifier,
onClick = { navController.navigate(NavigationRoutes.trackingSample) }
onClick = {
navController.navigate(
NavigationRoutes.updatableSample
)
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ch.srgssr.pillarbox.demo.ui.showcases.adaptive.AdaptivePlayerHome
import ch.srgssr.pillarbox.demo.ui.showcases.multiplayer.MultiPlayer
import ch.srgssr.pillarbox.demo.ui.showcases.story.StoryHome
import ch.srgssr.pillarbox.demo.ui.showcases.tracking.TrackingToggleSample
import ch.srgssr.pillarbox.demo.ui.showcases.updatable.UpdatableMediaItemView

/**
* Inject Showcases Navigation
Expand All @@ -39,6 +40,9 @@ fun NavGraphBuilder.showCasesNavGraph(navController: NavController) {
composable(NavigationRoutes.trackingSample, DemoPageView("tracking toggle", Levels)) {
TrackingToggleSample()
}
composable(NavigationRoutes.updatableSample, DemoPageView("updatable item", Levels)) {
UpdatableMediaItemView()
}
}

private val Levels = listOf("app", "pillarbox", "showcase")
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2023. SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.ui.showcases.updatable

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewmodel.compose.viewModel
import ch.srgssr.pillarbox.ui.extension.currentMediaMetadataAsState
import ch.srgssr.pillarbox.ui.widget.player.PlayerSurface

/**
* Updatable media item view
*/
@Composable
fun UpdatableMediaItemView() {
val updatableMediaItemViewModel: UpdatableMediaItemViewModel = viewModel()
val player = updatableMediaItemViewModel.player
val currentItem by player.currentMediaMetadataAsState()
PlayerSurface(player = player) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.TopStart) {
Text(
color = Color.Green,
text = "${currentItem.title}"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.ui.showcases.updatable

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import androidx.media3.common.MediaItem
import androidx.media3.ui.PlayerNotificationManager
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.player.notification.PillarboxMediaDescriptionAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timer
import kotlin.time.Duration.Companion.seconds

/**
* Updatable media item view model
*
* This demo demonstrate how to update an existing [MediaItem.mediaMetadata].
*
* @constructor
*
* @param application The application
*/
class UpdatableMediaItemViewModel(application: Application) : AndroidViewModel(application) {
/**
* The player
*/
val player = PlayerModule.provideDefaultPlayer(application)
private val notificationManager: PlayerNotificationManager
private val timer: Timer
private val baseTitle = "Update title"
private var counter = 0

init {
player.prepare()
player.setMediaItem(DemoItem.OnDemandHorizontalVideo.toMediaItem())
player.play()
notificationManager = PlayerNotificationManager.Builder(application, NOTIFICATION_ID, CHANNEL_ID)
.setChannelNameResourceId(androidx.media3.session.R.string.default_notification_channel_name)
.setMediaDescriptionAdapter(PillarboxMediaDescriptionAdapter(context = application, pendingIntent = null))
.build()
notificationManager.setPlayer(player)

timer = timer(name = "update-item", period = 3.seconds.inWholeMilliseconds) {
viewModelScope.launch(Dispatchers.Main) {
val currentMediaItem = player.currentMediaItem
currentMediaItem?.let {
// when localConfiguration is not null, it means the urn has loaded a playable media url.
if (it.localConfiguration != null) {
updateTitle(it, "$baseTitle - $counter")
counter++
}
}
}
}
}

private fun updateTitle(mediaItem: MediaItem, title: String) {
val updatedMediaItem = mediaItem.buildUpon()
.setMediaMetadata(
mediaItem.mediaMetadata.buildUpon()
.setTitle(title)
.build()
)
.build()
player.replaceMediaItem(player.currentMediaItemIndex, updatedMediaItem)
}

override fun onCleared() {
super.onCleared()
timer.cancel()
notificationManager.setPlayer(null)
player.release()
}

companion object {
private const val CHANNEL_ID = "DemoChannel"
private const val NOTIFICATION_ID = 456
}
}
7 changes: 3 additions & 4 deletions pillarbox-demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
<string name="auto">MediaController (Android Auto)</string>
<string name="adaptive">Resizable player</string>
<string name="player_swap">Multi player</string>
<string name="embeddings">Embeddings</string>
<string name="system_integration">System integration</string>
<string name="exoplayer">Exoplayer integration</string>
<string name="tracking">Tracking</string>
<string name="misc">Misc</string>
<string name="integrations">Integrations</string>
<string name="tracker_example">Tracking toggle sample</string>
<string name="exoplayer_view">Exoplayer View</string>
<string name="enter_url_or_urn">Enter a URL or URN</string>
Expand All @@ -25,4 +23,5 @@
<string name="production">Production</string>
<string name="stage">Stage</string>
<string name="test">Test</string>
<string name="update_media_item_example">Updatable media metadata</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ class PillarboxMediaSource(
}
}

/**
* Can update media item
*
* FIXME Test when using MediaController.
*
* @param mediaItem
* @return
*/
override fun canUpdateMediaItem(mediaItem: MediaItem): Boolean {
if (mediaItem.mediaId != this.mediaItem.mediaId || mediaItem.localConfiguration != this.mediaItem.localConfiguration) return false
return true
}

override fun updateMediaItem(mediaItem: MediaItem) {
this.mediaItem = mediaItem
}

@Suppress("TooGenericExceptionCaught")
override fun maybeThrowSourceInfoRefreshError() {
pendingError?.let {
Expand Down

0 comments on commit e210118

Please sign in to comment.