Skip to content

Commit

Permalink
400 single showcase file (#410)
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 committed Jan 25, 2024
1 parent d2ac168 commit cb8ae66
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.media3.common.Player
import ch.srgssr.pillarbox.analytics.SRGAnalytics
import ch.srgssr.pillarbox.core.business.integrationlayer.service.IlHost
Expand Down Expand Up @@ -72,21 +72,17 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection {
val ilHost = (intent.extras?.getSerializable(ARG_IL_HOST) as URL?) ?: IlHost.DEFAULT
playerViewModel = ViewModelProvider(this, factory = SimplePlayerViewModel.Factory(application, ilHost))[SimplePlayerViewModel::class.java]
readIntent(intent)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) {
SRGAnalytics.trackPagView(DemoPageView("simple player", levels = listOf("app", "pillarbox")))
}
}
lifecycleScope.launchWhenCreated {
playerViewModel.pictureInPictureRatio.collectLatest {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && playerViewModel.pictureInPictureEnabled.value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
lifecycleScope.launch {
playerViewModel.pictureInPictureRatio.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED).collectLatest {
val params = PictureInPictureParams.Builder()
.setAspectRatio(playerViewModel.pictureInPictureRatio.value)
.setAspectRatio(it)
.build()
setPictureInPictureParams(params)
}
}
}

// Bind PlaybackService to allow background playback and MediaNotification.
bindPlaybackService()
setContent {
Expand Down Expand Up @@ -166,6 +162,11 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection {
}
}

override fun onResume() {
super.onResume()
SRGAnalytics.trackPagView(DemoPageView("simple player", levels = listOf("app", "pillarbox")))
}

override fun onStart() {
super.onStart()
playerViewModel.player.play()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import ch.srgssr.pillarbox.demo.shared.ui.NavigationRoutes
import ch.srgssr.pillarbox.demo.ui.showcases.integrations.ExoPlayerShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.layouts.SimpleLayoutShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.layouts.StoryLayoutShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.misc.AdaptivePlayerShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.misc.MultiPlayerShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.misc.ResizablePlayerShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.misc.SmoothSeekingShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.misc.StartAtGivenTimeShowcase
import ch.srgssr.pillarbox.demo.ui.showcases.misc.TrackingToggleShowcase
Expand All @@ -33,7 +33,7 @@ fun NavGraphBuilder.showcasesNavGraph(navController: NavController) {
SimpleLayoutShowcase()
}
composable(NavigationRoutes.adaptive, DemoPageView("adaptive player", Levels)) {
AdaptivePlayerShowcase()
ResizablePlayerShowcase()
}
composable(NavigationRoutes.playerSwap, DemoPageView("multiplayer", Levels)) {
MultiPlayerShowcase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.media3.common.Player
import ch.srgssr.pillarbox.analytics.SRGAnalytics
import ch.srgssr.pillarbox.demo.DemoPageView
Expand All @@ -43,16 +43,11 @@ class MediaControllerActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) {
SRGAnalytics.trackPagView(DemoPageView("media controller player", levels = listOf("app", "pillarbox")))
}
}
lifecycleScope.launchWhenCreated {
controllerViewModel.pictureInPictureRatio.collectLatest {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && controllerViewModel.pictureInPictureEnabled.value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
lifecycleScope.launch {
controllerViewModel.pictureInPictureRatio.flowWithLifecycle(lifecycle, Lifecycle.State.CREATED).collectLatest {
val params = PictureInPictureParams.Builder()
.setAspectRatio(controllerViewModel.pictureInPictureRatio.value)
.setAspectRatio(it)
.build()
setPictureInPictureParams(params)
}
Expand Down Expand Up @@ -111,4 +106,9 @@ class MediaControllerActivity : ComponentActivity() {
controllerViewModel.pictureInPictureEnabled.value = isInPictureInPictureMode
}
}

override fun onResume() {
super.onResume()
SRGAnalytics.trackPagView(DemoPageView("media controller player", levels = listOf("app", "pillarbox")))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@
*/
package ch.srgssr.pillarbox.demo.ui.showcases.layouts

import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerDefaults
import androidx.compose.foundation.pager.PagerSnapDistance
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.LifecycleStartEffect
import androidx.media3.common.C
import androidx.media3.common.Player
import androidx.media3.ui.AspectRatioFrameLayout
import androidx.media3.ui.PlayerView
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.data.Playlist
import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.player.PillarboxPlayer
import ch.srgssr.pillarbox.ui.ScaleMode
import ch.srgssr.pillarbox.ui.widget.player.PlayerSurface

/**
* A sample trying to reproduce story like TikTok.
Expand All @@ -29,13 +36,19 @@ import ch.srgssr.pillarbox.player.PillarboxPlayer
@Composable
fun SimpleStory() {
val playlist = remember {
Playlist.VideoUrls
Playlist.VideoUrns
}

val pagerState = rememberPagerState { playlist.items.size }
HorizontalPager(
modifier = Modifier.fillMaxHeight(),
key = { page -> playlist.items[page].uri },
beyondBoundsPageCount = 1,
flingBehavior = PagerDefaults.flingBehavior(
state = pagerState,
pagerSnapDistance = PagerSnapDistance.atMost(0),
snapAnimationSpec = spring(stiffness = Spring.StiffnessHigh)
),
state = pagerState
) { page ->
SimpleStoryPlayer(demoItem = playlist.items[page], isPlaying = pagerState.currentPage == page)
Expand All @@ -51,29 +64,30 @@ fun SimpleStory() {
*/
@Composable
private fun SimpleStoryPlayer(demoItem: DemoItem, isPlaying: Boolean = false) {
AndroidView(
factory = { context ->
val player = PillarboxPlayer(
context = context,
mediaItemSource = PlayerModule.provideMixedItemSource(context),
loadControl = StoryLoadControl.build()
).apply {
setMediaItem(demoItem.toMediaItem())
prepare()
videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
repeatMode = Player.REPEAT_MODE_ONE
playWhenReady = isPlaying
}

PlayerView(context).apply {
hideController()
useController = false
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
this.player = player
}
},
onRelease = {
it.player?.release()
val context = LocalContext.current
val player = remember(demoItem) {
PlayerModule.provideDefaultPlayer(context).apply {
setMediaItem(demoItem.toMediaItem())
videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
repeatMode = Player.REPEAT_MODE_ONE
}
}
DisposableEffect(player) {
player.prepare()
onDispose {
player.release()
}
}
PlayerSurface(
modifier = Modifier.fillMaxSize(),
player = player,
scaleMode = ScaleMode.Crop
)

LifecycleStartEffect(isPlaying) {
player.playWhenReady = isPlaying
onStopOrDispose {
player.pause()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@ class StoryViewModel(application: Application) : AndroidViewModel(application) {
PillarboxPlayer(
context = application,
mediaItemSource = mediaItemSource,
loadControl = StoryLoadControl.build(),
mediaItemTrackerProvider = itemTrackerProvider
),
PillarboxPlayer(
context = application,
mediaItemSource = mediaItemSource,
loadControl = StoryLoadControl.build(),
mediaItemTrackerProvider = itemTrackerProvider
),
PillarboxPlayer(
context = application,
mediaItemSource = mediaItemSource,
loadControl = StoryLoadControl.build(),
mediaItemTrackerProvider = itemTrackerProvider
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.media3.common.Player
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.demo.ui.player.PlayerView
import ch.srgssr.pillarbox.demo.ui.theme.paddings

Expand All @@ -30,13 +35,40 @@ import ch.srgssr.pillarbox.demo.ui.theme.paddings
*/
@Composable
fun MultiPlayerShowcase() {
val multiPlayerViewModel: MultiPlayerViewModel = viewModel()
var swapLeftRight by remember {
mutableStateOf(false)
}
val playerLeft = multiPlayerViewModel.getPlayerLeft(swapLeftRight)
val playerRight = multiPlayerViewModel.getPlayerRight(swapLeftRight)

val context = LocalContext.current
val playerOne = remember {
PlayerModule.provideDefaultPlayer(context).apply {
repeatMode = Player.REPEAT_MODE_ONE
setMediaItem(DemoItem.LiveVideo.toMediaItem())
prepare()
}
}
val playerTwo = remember {
PlayerModule.provideDefaultPlayer(context).apply {
repeatMode = Player.REPEAT_MODE_ONE
setMediaItem(DemoItem.DvrVideo.toMediaItem())
prepare()
}
}
DisposableEffect(Unit) {
onDispose {
playerOne.release()
playerTwo.release()
}
}
LifecycleResumeEffect(Unit) {
playerOne.play()
playerTwo.play()
onPauseOrDispose {
playerOne.pause()
playerTwo.pause()
}
}
val leftPlayer = if (swapLeftRight) playerTwo else playerOne
val rightPlayer = if (swapLeftRight) playerOne else playerTwo
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = { swapLeftRight = !swapLeftRight }) {
Text(text = "Swap players")
Expand All @@ -46,16 +78,14 @@ fun MultiPlayerShowcase() {
PlayerView(
modifier = Modifier
.weight(1.0f)
.aspectRatio(AspectRatio)
.padding(MaterialTheme.paddings.mini),
player = playerLeft
player = leftPlayer,
)
PlayerView(
modifier = Modifier
.weight(1.0f)
.aspectRatio(AspectRatio)
.padding(MaterialTheme.paddings.mini),
player = playerRight
player = rightPlayer
)
}
} else {
Expand All @@ -65,14 +95,14 @@ fun MultiPlayerShowcase() {
.weight(1.0f)
.aspectRatio(AspectRatio)
.padding(MaterialTheme.paddings.mini),
player = playerLeft
player = leftPlayer
)
PlayerView(
modifier = Modifier
.weight(1.0f)
.aspectRatio(AspectRatio)
.padding(MaterialTheme.paddings.mini),
player = playerRight
player = rightPlayer
)
}
}
Expand Down
Loading

0 comments on commit cb8ae66

Please sign in to comment.