diff --git a/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/di/PlayerModule.kt b/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/di/PlayerModule.kt index 44eb316a4..45ffe3074 100644 --- a/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/di/PlayerModule.kt +++ b/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/di/PlayerModule.kt @@ -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)) } /** diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/MainNavigation.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/MainNavigation.kt index 643ca9af1..14726769b 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/MainNavigation.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/MainNavigation.kt @@ -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"))) { diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/integrationLayer/ContentListsView.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/integrationLayer/ContentListsView.kt index 9597e30aa..bd72c0376 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/integrationLayer/ContentListsView.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/integrationLayer/ContentListsView.kt @@ -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 -> { @@ -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 -> { diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt index f9faa4686..8d2d99f9f 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerActivity.kt @@ -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 @@ -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 @@ -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. @@ -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) { @@ -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) { @@ -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) } } } diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt index 51fc3db82..4233c8e76 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt @@ -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 @@ -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) @@ -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 create(modelClass: Class): T { + return SimplePlayerViewModel(application, ilHost) as T + } + } }