Skip to content

Commit

Permalink
Also change ILHost for the player
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Dec 6, 2023
1 parent 17a15b3 commit a75bf87
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@ import java.net.URL
*/
object PlayerModule {

private fun provideIntegrationLayerItemSource(context: Context): MediaCompositionMediaItemSource =
private fun provideIntegrationLayerItemSource(context: Context, ilHost: URL = IlHost.DEFAULT): MediaCompositionMediaItemSource =
MediaCompositionMediaItemSource(
mediaCompositionDataSource = DefaultMediaCompositionDataSource(vector = context.getVector()),
mediaCompositionDataSource = DefaultMediaCompositionDataSource(vector = context.getVector(), baseUrl = ilHost),
)

/**
* Provide mixed item source that load Url and Urn
*/
fun provideMixedItemSource(context: Context): MixedMediaItemSource = MixedMediaItemSource(
provideIntegrationLayerItemSource(context)
fun provideMixedItemSource(
context: Context,
ilHost: URL = IlHost.DEFAULT
): MixedMediaItemSource = MixedMediaItemSource(
provideIntegrationLayerItemSource(context, ilHost)
)

/**
* Provide default player that allow to play urls and urns content from the SRG
*/
fun provideDefaultPlayer(context: Context): PillarboxPlayer {
return DefaultPillarbox(context = context, mediaItemSource = provideMixedItemSource(context))
fun provideDefaultPlayer(context: Context, ilHost: URL = IlHost.DEFAULT): PillarboxPlayer {
return DefaultPillarbox(context = context, mediaItemSource = provideMixedItemSource(context, ilHost))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fun MainNavigation() {
navigation(startDestination = NavigationRoutes.contentLists, route = HomeDestination.Lists.route) {
val ilRepository = PlayerModule.createIlRepository(context, ilHost)

listNavGraph(navController, ilRepository)
listNavGraph(navController, ilRepository, ilHost)
}

composable(HomeDestination.Info.route, DemoPageView("home", listOf("app", "pillarbox", "information"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ import ch.srgssr.pillarbox.demo.ui.composable
import ch.srgssr.pillarbox.demo.ui.player.SimplePlayerActivity
import ch.srgssr.pillarbox.demo.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.demo.ui.theme.paddings
import java.net.URL

private val defaultListsLevels = listOf("app", "pillarbox", "lists")

/**
* Build Navigation for integration layer list view
*/
fun NavGraphBuilder.listNavGraph(navController: NavController, ilRepository: ILRepository) {
fun NavGraphBuilder.listNavGraph(navController: NavController, ilRepository: ILRepository, ilHost: URL) {
val contentClick = { contentList: ContentList, content: Content ->
when (content) {
is Content.Show -> {
Expand All @@ -65,7 +66,7 @@ fun NavGraphBuilder.listNavGraph(navController: NavController, ilRepository: ILR

is Content.Media -> {
val item = DemoItem(title = content.title, uri = content.urn)
SimplePlayerActivity.startActivity(navController.context, item)
SimplePlayerActivity.startActivity(navController.context, item, ilHost)
}

is Content.Channel -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import android.os.Bundle
import android.os.IBinder
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
Expand All @@ -24,10 +23,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
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
import ch.srgssr.pillarbox.demo.DemoPageView
import ch.srgssr.pillarbox.demo.service.DemoPlaybackService
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
Expand All @@ -37,6 +38,7 @@ import ch.srgssr.pillarbox.demo.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.player.service.PlaybackService
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.net.URL

/**
* Simple player activity that can handle picture in picture.
Expand All @@ -49,7 +51,7 @@ import kotlinx.coroutines.launch
*/
class SimplePlayerActivity : ComponentActivity(), ServiceConnection {

private val playerViewModel: SimplePlayerViewModel by viewModels()
private lateinit var playerViewModel: SimplePlayerViewModel
private var layoutStyle: Int = LAYOUT_PLAYLIST

private fun readIntent(intent: Intent) {
Expand All @@ -66,6 +68,8 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
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) {
Expand Down Expand Up @@ -185,25 +189,27 @@ class SimplePlayerActivity : ComponentActivity(), ServiceConnection {
companion object {
private const val ARG_PLAYLIST = "ARG_PLAYLIST"
private const val ARG_LAYOUT = "ARG_LAYOUT"
private const val ARG_IL_HOST = "ARG_IL_HOST"
private const val LAYOUT_SIMPLE = 1
private const val LAYOUT_PLAYLIST = 0

/**
* Start activity [SimplePlayerActivity] with [playlist]
*/
fun startActivity(context: Context, playlist: Playlist) {
fun startActivity(context: Context, playlist: Playlist, ilHost: URL = IlHost.DEFAULT) {
val layoutStyle: Int = if (playlist.items.isEmpty() || playlist.items.size > 1) LAYOUT_PLAYLIST else LAYOUT_SIMPLE
val intent = Intent(context, SimplePlayerActivity::class.java)
intent.putExtra(ARG_PLAYLIST, playlist)
intent.putExtra(ARG_LAYOUT, layoutStyle)
intent.putExtra(ARG_IL_HOST, ilHost)
context.startActivity(intent)
}

/**
* Start activity [SimplePlayerActivity] with [demoItem]
* Start activity [SimplePlayerActivity] with DemoItem.
*/
fun startActivity(context: Context, item: DemoItem) {
startActivity(context, Playlist("UniqueItem", listOf(item)))
fun startActivity(context: Context, item: DemoItem, ilHost: URL = IlHost.DEFAULT) {
startActivity(context, Playlist("UniqueItem", listOf(item)), ilHost)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.app.Application
import android.util.Log
import android.util.Rational
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.media3.common.C
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
Expand All @@ -20,15 +22,16 @@ import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.player.extension.toRational
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.net.URL

/**
* Simple player view model than handle a PillarboxPlayer [player]
*/
class SimplePlayerViewModel(application: Application) : AndroidViewModel(application), Player.Listener {
class SimplePlayerViewModel(application: Application, ilHost: URL) : AndroidViewModel(application), Player.Listener {
/**
* Player as PillarboxPlayer
*/
val player = PlayerModule.provideDefaultPlayer(application)
val player = PlayerModule.provideDefaultPlayer(application, ilHost)

private val _pauseOnBackground = MutableStateFlow(true)
private val _displayNotification = MutableStateFlow(false)
Expand Down Expand Up @@ -176,4 +179,13 @@ class SimplePlayerViewModel(application: Application) : AndroidViewModel(applica
companion object {
private const val TAG = "PillarboxDemo"
}

/**
* Factory to create [SimplePlayerViewModel].
*/
class Factory(private val application: Application, private val ilHost: URL) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return SimplePlayerViewModel(application, ilHost) as T
}
}
}

0 comments on commit a75bf87

Please sign in to comment.