Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PillarboxCastPlayer and update the demo #875

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pillarbox-cast/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
plugins {
alias(libs.plugins.pillarbox.android.library)
alias(libs.plugins.pillarbox.android.library.compose)
Expand All @@ -6,6 +10,7 @@ plugins {
}

dependencies {
implementation(project(":pillarbox-player"))
implementation(platform(libs.androidx.compose.bom))
api(libs.androidx.compose.runtime)
api(libs.androidx.compose.ui)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.cast

import androidx.media3.cast.CastPlayer
import androidx.media3.cast.SessionAvailabilityListener
import androidx.media3.common.ForwardingPlayer
import ch.srgssr.pillarbox.player.PillarboxPlayer
import com.google.android.gms.cast.MediaQueueItem
import com.google.android.gms.cast.framework.CastContext

/**
* A [PillarboxPlayer] implementation that forwards calls to a [CastPlayer].
*
* It disables smooth seeking and tracking capabilities as these are not supported or relevant in the context of Cast playback.
*
* @param castPlayer The underlying [CastPlayer] instance to which method calls will be forwarded.
*/
class PillarboxCastPlayer(private val castPlayer: CastPlayer) : PillarboxPlayer, ForwardingPlayer(castPlayer) {
override var smoothSeekingEnabled: Boolean = false
set(value) {
field = false
}

override var trackingEnabled: Boolean = false
set(value) {
field = false
}

constructor(context: CastContext) : this(CastPlayer(context))

/**
* Returns the item that corresponds to the period with the given id, or `null` if no media queue or period with id [periodId] exist.
*
* @param periodId The id of the period ([getCurrentTimeline]) that corresponds to the item to get.
* @return The item that corresponds to the period with the given id, or `null` if no media queue or period with id [periodId] exist.
*/
fun getItem(periodId: Int): MediaQueueItem? {
return castPlayer.getItem(periodId)
}

/**
* Returns whether a cast session is available.
*/
fun isCastSessionAvailable(): Boolean {
return castPlayer.isCastSessionAvailable
}

/**
* Sets a listener for updates on the cast session availability.
*
* @param listener The [SessionAvailabilityListener], or `null` to clear the listener.
*/
fun setSessionAvailabilityListener(listener: SessionAvailabilityListener?) {
castPlayer.setSessionAvailabilityListener(listener)
}
}
2 changes: 1 addition & 1 deletion pillarbox-demo-cast/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
dependencies {
implementation(project(":pillarbox-demo-shared"))
implementation(project(":pillarbox-cast"))
implementation(project(":pillarbox-ui"))
implementation(libs.androidx.activity)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
Expand All @@ -17,7 +18,6 @@ dependencies {
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.runtime)
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.text)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.ui.unit)
Expand Down
5 changes: 5 additions & 0 deletions pillarbox-demo-cast/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) SRG SSR. All rights reserved.
~ License information is available from the LICENSE file.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".CastApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_pillarbox"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.demo.cast

import android.app.Application
import ch.srgssr.pillarbox.cast.getCastContext
import com.google.android.gms.cast.framework.CastContext

/**
* [CastApplication] initializes Google Cast functionality by setting up the [CastContext].
*/
class CastApplication : Application() {
override fun onCreate() {
super.onCreate()

getCastContext()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,65 @@ package ch.srgssr.pillarbox.demo.cast
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.FragmentActivity
import androidx.media3.cast.SessionAvailabilityListener
import androidx.media3.common.MediaItem
import androidx.media3.common.MimeTypes
import ch.srgssr.pillarbox.cast.PillarboxCastPlayer
import ch.srgssr.pillarbox.cast.getCastContext
import ch.srgssr.pillarbox.cast.widget.CastButton
import ch.srgssr.pillarbox.demo.cast.ui.theme.PillarboxTheme
import ch.srgssr.pillarbox.ui.exoplayer.ExoPlayerView

/**
* Activity showing how to use Cast with Pillarbox.
*/
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Force creation CastContext
getCastContext()

enableEdgeToEdge()

setContent {
val player = remember {
PillarboxCastPlayer(getCastContext()).apply {
val mediaItem = MediaItem.Builder()
.setMimeType(MimeTypes.VIDEO_MP4)
.setUri("https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd")
.build()

setSessionAvailabilityListener(object : SessionAvailabilityListener {
override fun onCastSessionAvailable() {
setMediaItem(mediaItem)
}

override fun onCastSessionUnavailable() {
release()
}
})
}
}

PillarboxTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
floatingActionButton = {
CastButton()
}
},
) { innerPadding ->
Box(
modifier = Modifier.padding(innerPadding)
) {
Text(text = "Demo showing the cast button!")
}
ExoPlayerView(
player = player,
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
)
}
}
}
Expand Down
Loading