Skip to content

Commit

Permalink
748 simplify player creation (#760)
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 Oct 23, 2024
1 parent 412e971 commit e6932ec
Show file tree
Hide file tree
Showing 37 changed files with 1,041 additions and 853 deletions.
37 changes: 26 additions & 11 deletions pillarbox-core-business/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ More information can be found in the [top level README](https://github.com/SRGSS
To play a URN content with [`PillarboxPlayer`][pillarbox-player-source], you have to create it like this:

```kotlin
val player = DefaultPillarbox(context)
val player = PillarboxExoPlayer(context)
// Make the player ready to play content
player.prepare()
// Will start playback when a MediaItem is ready to play
Expand All @@ -41,13 +41,27 @@ player.play()
### Create a `MediaItem` with URN

To tell [`PillarboxPlayer`][pillarbox-player-source] to load a specific [`MediaItem`][media-item-documentation], it has to be created with
[`SRGMediaItemBuilder`][srg-media-item-builder-source]:
[`SRGMediaItem`][srg-media-item-source]:

```kotlin
val urn = "urn:rts:video:12345"
val mediaItem = SRGMediaItemBuilder(urn).build()
val mediaItem: MediaItem = SRGMediaItem(urn)

player.setMediaItem(mediaItem)
// Content on stage
val mediaItemOnStage: MediaItem = SRGMediaItem(urn) {
setHost(IlHost.Stage)
}

// Content with TV Vector
val mediaItemWithVector : MediaItem = SRGMediaItem(urn) {
setVector(Vector.TV)
}

// Compute Vector from Context
val vector = context.getVector()
val mediaItemWithVector : MediaItem = SRGMediaItem(urn) {
setVector(vector)
}
```

### Handle error
Expand Down Expand Up @@ -94,24 +108,25 @@ class CustomMediaCompositionService : MediaCompositionService {
}
```

Then, pass it to [`DefaultPillarbox`][default-pillarbox-source]:
Then, pass it to [`PillarboxExoPlayer`][pillarbox-exo-player-source]:

```kotlin
val player = DefaultPillarbox(
context = context,
mediaCompositionService = CustomMediaCompositionService(),
)
val player = PillarboxExoPlayer(context) {
srgAssetLoader(context) {
mediaCompositionService(CustomMediaCompositionService())
}
}
```

[block-reason-exception-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/exception/BlockReasonException.kt
[default-pillarbox-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/DefaultPillarbox.kt
[media-composition-service-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/integrationlayer/service/MediaCompositionService.kt
[media-composition-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/integrationlayer/data/MediaComposition.kt
[media-item-documentation]: https://developer.android.com/reference/androidx/media3/common/MediaItem
[media-source-documentation]: https://developer.android.com/reference/androidx/media3/exoplayer/source/MediaSource
[pillarbox-exo-player-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/PillarboxSRG.kt
[pillarbox-media-source-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/source/PillarboxMediaSource.kt
[pillarbox-player-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/PillarboxPlayer.kt
[playback-exception-documentation]: https://developer.android.com/reference/androidx/media3/common/PlaybackException
[resource-not-found-exception-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/exception/ResourceNotFoundException.kt
[spherical-surface-showcase]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/showcases/misc/SphericalSurfaceShowcase.kt
[srg-media-item-builder-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/SRGMediaItemBuilder.kt
[srg-media-item-source]: https://github.com/SRGSSR/pillarbox-android/tree/main/pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/SRGMediaItem.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.core.business

import android.content.Context
import androidx.media3.exoplayer.ExoPlayer
import ch.srgssr.pillarbox.core.business.source.SRGAssetLoader
import ch.srgssr.pillarbox.core.business.source.SRGAssetLoaderConfig
import ch.srgssr.pillarbox.player.PillarboxBuilder
import ch.srgssr.pillarbox.player.PillarboxDsl
import ch.srgssr.pillarbox.player.PillarboxExoPlayer
import ch.srgssr.pillarbox.player.PlayerConfig
import kotlin.time.Duration.Companion.seconds

/**
* Pillarbox ExoPlayer configured for the SRG SSR.
*
* @param context The [Context].
* @param builder The builder.
* @receiver [SRG.Builder].
* @return The configured [PillarboxExoplayer] for SRG SSR.
*/
@Suppress("FunctionName")
@PillarboxDsl
fun PillarboxExoplayer(
context: Context,
builder: SRG.Builder.() -> Unit = {},
): PillarboxExoPlayer {
return SRG.create()
.apply(builder)
.create(context)
}

/**
* Pillarbox player configuration for the SRG.
* It sets up all SRG components by default.
*/
@Suppress("MatchingDeclarationName")
object SRG : PlayerConfig<SRG.Builder> {

override fun create(): Builder {
return Builder()
}

/**
* Builder for the SRG.
*/
class Builder : PillarboxBuilder() {
init {
val url = if (BuildConfig.DEBUG) "https://dev.monitoring.pillarbox.ch/api/events" else "https://monitoring.pillarbox.ch/api/events"
monitoring(url)
seekForwardIncrement(30.seconds)
seekBackwardIncrement(10.seconds)
}

private var srgAssetLoader: SRGAssetLoader? = null

/**
* Configure a [SRGAssetLoader].
*
* @param context The [Context].
* @param block The block to configure a [SRGAssetLoader].
* @receiver [SRGAssetLoaderConfig].
*/
fun srgAssetLoader(context: Context, block: SRGAssetLoaderConfig.() -> Unit) {
check(srgAssetLoader == null)
srgAssetLoader = SRGAssetLoader(context, block)
.also(::addAssetLoader)
}

override fun createExoPlayerBuilder(context: Context): ExoPlayer.Builder {
if (srgAssetLoader == null) srgAssetLoader(context) {}
return super.createExoPlayerBuilder(context)
}
}
}
Loading

0 comments on commit e6932ec

Please sign in to comment.