-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to AndroidX media3 1.2.0 (#364)
Co-authored-by: Gaëtan Muller <[email protected]>
- Loading branch information
Showing
8 changed files
with
171 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
35 changes: 35 additions & 0 deletions
35
...o/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/updatable/UpdatableMediaItemView.kt
This file contains 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,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}" | ||
) | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
.../main/java/ch/srgssr/pillarbox/demo/ui/showcases/updatable/UpdatableMediaItemViewModel.kt
This file contains 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,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 | ||
} | ||
} |
This file contains 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
This file contains 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