-
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.
Convenient corebusiness player (#244)
- Loading branch information
Showing
5 changed files
with
175 additions
and
15 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/DefaultPillarbox.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,101 @@ | ||
/* | ||
* Copyright (c) 2023. 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.common.TrackSelectionParameters | ||
import androidx.media3.exoplayer.DefaultLoadControl | ||
import androidx.media3.exoplayer.DefaultRenderersFactory | ||
import androidx.media3.exoplayer.ExoPlayer | ||
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory | ||
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector | ||
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter | ||
import ch.srgssr.pillarbox.core.business.akamai.AkamaiTokenDataSource | ||
import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultMediaCompositionDataSource | ||
import ch.srgssr.pillarbox.core.business.integrationlayer.service.Vector.getVector | ||
import ch.srgssr.pillarbox.core.business.tracker.DefaultMediaItemTrackerRepository | ||
import ch.srgssr.pillarbox.player.PillarboxPlayer | ||
import ch.srgssr.pillarbox.player.SeekIncrement | ||
import ch.srgssr.pillarbox.player.extension.setPreferredAudioRoleFlagsToAccessibilityManagerSettings | ||
import ch.srgssr.pillarbox.player.extension.setSeekIncrements | ||
import ch.srgssr.pillarbox.player.source.PillarboxMediaSourceFactory | ||
import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerProvider | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* DefaultPillarbox convenient class to create [PillarboxPlayer] that suit SRG needs. | ||
*/ | ||
object DefaultPillarbox { | ||
private val defaultSeekIncrement = SeekIncrement(backward = 10.seconds, forward = 30.seconds) | ||
|
||
/** | ||
* Invoke create an instance of [PillarboxPlayer] | ||
* | ||
* @param context The context. | ||
* @param seekIncrement The seek increment. | ||
* @param mediaItemTrackerRepository The provider of MediaItemTracker. | ||
* @param trackerProvider The [TrackerDataProvider] to customize tracker data. | ||
* @return [PillarboxPlayer] suited for SRG. | ||
*/ | ||
operator fun invoke( | ||
context: Context, | ||
seekIncrement: SeekIncrement = defaultSeekIncrement, | ||
mediaItemTrackerRepository: MediaItemTrackerProvider = DefaultMediaItemTrackerRepository(), | ||
trackerProvider: TrackerDataProvider? = null | ||
): PillarboxPlayer { | ||
return PillarboxPlayer( | ||
Builder(context, seekIncrement, trackerProvider), | ||
mediaItemTrackerProvider = mediaItemTrackerRepository | ||
) | ||
} | ||
|
||
/** | ||
* Builder convenient class to create a [ExoPlayer.Builder] that suit SRG needs. | ||
*/ | ||
object Builder { | ||
/** | ||
* Invoke | ||
* | ||
* @param context The context. | ||
* @param seekIncrement The seek increment. | ||
* @param trackerProvider The [TrackerDataProvider] to customize tracker data. | ||
* @return [ExoPlayer.Builder] suited for SRG. | ||
*/ | ||
operator fun invoke( | ||
context: Context, | ||
seekIncrement: SeekIncrement = defaultSeekIncrement, | ||
trackerProvider: TrackerDataProvider? = null | ||
): ExoPlayer.Builder { | ||
return ExoPlayer.Builder(context) | ||
.setUsePlatformDiagnostics(false) | ||
.setSeekIncrements(seekIncrement) | ||
.setRenderersFactory( | ||
DefaultRenderersFactory(context) | ||
.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF) | ||
.setEnableDecoderFallback(true) | ||
) | ||
.setBandwidthMeter(DefaultBandwidthMeter.getSingletonInstance(context)) | ||
.setLoadControl(DefaultLoadControl()) | ||
.setMediaSourceFactory( | ||
PillarboxMediaSourceFactory( | ||
mediaItemSource = MediaCompositionMediaItemSource( | ||
DefaultMediaCompositionDataSource(vector = context.getVector()), | ||
trackerProvider | ||
), | ||
defaultMediaSourceFactory = DefaultMediaSourceFactory(AkamaiTokenDataSource.Factory()) | ||
) | ||
) | ||
.setTrackSelector( | ||
DefaultTrackSelector( | ||
context, | ||
TrackSelectionParameters.Builder(context) | ||
.setPreferredAudioRoleFlagsToAccessibilityManagerSettings(context) | ||
.build() | ||
) | ||
) | ||
.setDeviceVolumeControlEnabled(true) // allow player to control device volume | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
pillarbox-core-business/src/main/java/ch/srgssr/pillarbox/core/business/MediaItemUrn.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,36 @@ | ||
/* | ||
* Copyright (c) 2023. SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.core.business | ||
|
||
import android.net.Uri | ||
import androidx.media3.common.MediaItem | ||
import androidx.media3.common.MediaMetadata | ||
|
||
/** | ||
* Create [MediaItem] for Pillarbox from a urn. | ||
*/ | ||
object MediaItemUrn { | ||
/** | ||
* Invoke | ||
* | ||
* @param urn The media urn to play. | ||
* @param title The optional title to display.. | ||
* @param subtitle The optional subtitle to display. | ||
* @param artworkUri The artworkUri image uri. | ||
* @return MediaItem. | ||
*/ | ||
operator fun invoke(urn: String, title: String? = null, subtitle: String? = null, artworkUri: Uri? = null): MediaItem { | ||
return MediaItem.Builder() | ||
.setMediaId(urn) | ||
.setMediaMetadata( | ||
MediaMetadata.Builder() | ||
.setTitle(title) | ||
.setSubtitle(subtitle) | ||
.setArtworkUri(artworkUri) | ||
.build() | ||
) | ||
.build() | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/extension/ExoPlayerBuilder.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,20 @@ | ||
/* | ||
* Copyright (c) 2023. SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.player.extension | ||
|
||
import androidx.media3.exoplayer.ExoPlayer | ||
import ch.srgssr.pillarbox.player.SeekIncrement | ||
|
||
/** | ||
* Set seek increments | ||
* | ||
* @param seekIncrement The seek increments to set. | ||
* @return this | ||
*/ | ||
fun ExoPlayer.Builder.setSeekIncrements(seekIncrement: SeekIncrement): ExoPlayer.Builder { | ||
setSeekForwardIncrementMs(seekIncrement.forward.inWholeMilliseconds) | ||
setSeekBackIncrementMs(seekIncrement.backward.inWholeMilliseconds) | ||
return this | ||
} |