-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Chromecast to demo * Update player.cast on source change * Fix Chromecast discovery in demo app * Keep controls visible while casting * Add guide on setting up Chromecast
- Loading branch information
1 parent
f288f24
commit d8ecb96
Showing
10 changed files
with
202 additions
and
15 deletions.
There are no files selected for viewing
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
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/theoplayer/android/ui/demo/CastOptionsProvider.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,30 @@ | ||
package com.theoplayer.android.ui.demo | ||
|
||
import android.content.Context | ||
import com.google.android.gms.cast.CastMediaControlIntent | ||
import com.google.android.gms.cast.framework.CastOptions | ||
import com.google.android.gms.cast.framework.OptionsProvider | ||
import com.google.android.gms.cast.framework.SessionProvider | ||
import com.google.android.gms.cast.framework.media.CastMediaOptions | ||
import com.google.android.gms.cast.framework.media.NotificationOptions | ||
|
||
class CastOptionsProvider : OptionsProvider { | ||
override fun getCastOptions(context: Context): CastOptions { | ||
val notificationOptions = NotificationOptions.Builder() | ||
.setTargetActivityClassName(MainActivity::class.java.name) | ||
.build() | ||
|
||
val castMediaOptions = CastMediaOptions.Builder() | ||
.setNotificationOptions(notificationOptions) | ||
.build() | ||
|
||
return CastOptions.Builder() | ||
.setReceiverApplicationId(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID) | ||
.setCastMediaOptions(castMediaOptions) | ||
.build() | ||
} | ||
|
||
override fun getAdditionalSessionProviders(context: Context): List<SessionProvider>? { | ||
return null | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
|
||
<style name="Theme.THEOplayerAndroidUI" parent="android:Theme.Material.Light.NoActionBar"> | ||
<!-- This MUST be a Theme.AppCompat theme for the Chromecast MediaRouteChooserDialog to work! --> | ||
<style name="Theme.THEOplayerAndroidUI" parent="Theme.AppCompat.Light.NoActionBar"> | ||
<item name="android:statusBarColor">@color/purple_700</item> | ||
</style> | ||
</resources> |
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,105 @@ | ||
--- | ||
sidebar_position: 2 | ||
--- | ||
|
||
# Setting up Chromecast | ||
|
||
The Open Video UI for Android has integrated support for Chromecast. | ||
|
||
- When using the `DefaultUI`, the Chromecast button is automatically added in the top right corner | ||
of the player. | ||
- When creating a custom UI using a `UIController`, you can add a `ChromecastButton` component | ||
anywhere you like. | ||
You should also add a `ChromecastDisplay` to show a "Playing on Chromecast" message while casting. | ||
|
||
However, you need to perform some additional setup to get it fully working. | ||
|
||
## Install the THEOplayer Cast integration | ||
|
||
The Chromecast support requires the THEOplayer Cast integration to be included in your app. | ||
We'll also need to interact with the Cast Application Framework directly, so we'll include that too: | ||
|
||
```groovy title="build.gradle" | ||
dependencies { | ||
implementation "com.theoplayer.theoplayer-sdk-android:core:7.+" | ||
// highlight-next-line | ||
implementation "com.theoplayer.theoplayer-sdk-android:integration-cast:7.+" | ||
// highlight-next-line | ||
implementation "com.google.android.gms:play-services-cast-framework:21.5.0" | ||
implementation "com.theoplayer.android-ui:android-ui:1.+" | ||
} | ||
``` | ||
|
||
Create the player manually using `rememberPlayer()`, and then create and add the cast integration: | ||
|
||
```kotlin title="MainActivity.kt" | ||
import com.theoplayer.android.api.cast.CastConfiguration | ||
import com.theoplayer.android.api.cast.CastIntegrationFactory | ||
import com.theoplayer.android.api.cast.CastStrategy | ||
|
||
setContent { | ||
val player = rememberPlayer() | ||
LaunchedEffect(player) { | ||
player.theoplayerView?.let { theoplayerView -> | ||
// Add Chromecast integration | ||
val castConfiguration = CastConfiguration.Builder().apply { | ||
castStrategy(CastStrategy.AUTO) | ||
}.build() | ||
theoplayerView.player.addIntegration( | ||
CastIntegrationFactory.createCastIntegration(theoplayerView, castConfiguration) | ||
) | ||
} | ||
} | ||
|
||
DefaultUI( | ||
player = player | ||
) | ||
} | ||
``` | ||
|
||
## Initialize the `CastContext` during activity creation | ||
|
||
The Cast Application Framework handles automatic discovery of Chromecast receivers. | ||
However, this only works correctly if the `CastContext` is initialized immediately when | ||
your app's activity is constructed. | ||
|
||
Therefore, make sure to call `CastContext.getSharedInstance(this)` inside `Activity.onCreate()`: | ||
|
||
```kotlin title="MainActivity.kt" | ||
import com.google.android.gms.cast.framework.CastContext | ||
|
||
class MainActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
// Initialize Chromecast immediately, for automatic receiver discovery to work correctly. | ||
CastContext.getSharedInstance(this) | ||
|
||
setContent { | ||
// ... | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Use an `AppCompat` theme | ||
|
||
The Cast Application Framework creates dialogs such as | ||
[MediaRouteChooserDialog](https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteChooserDialog) | ||
and [MediaRouteControllerDialog](https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteControllerDialog) | ||
to start and control a cast session. However, because these dialogs inherit | ||
from [AppCompatDialog](https://developer.android.com/reference/androidx/appcompat/app/AppCompatDialog), | ||
you need to use theme based on `Theme.AppCompat` in your app: | ||
|
||
```xml title="src/main/res/values/themes.xml" | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<!-- don't do this: | ||
<style name="Theme.MyAppTheme" parent="android:Theme.Material.Light.NoActionBar"> | ||
instead, do this: | ||
--> | ||
<style name="Theme.MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | ||
<!-- your app's theme colors go here --> | ||
</style> | ||
</resources> | ||
``` |
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
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