Chromecast (Android)

The Dailymotion Cast SDK allows you to easily integrate a casting feature on your Dailymotion Player with minimum requirements.

👍

Chromecast module is optional

The Android SDK can be integrated without the Chromecast module. It is optional and can be excluded based on your project requirements.

💡

Demo

Check our sample apps for an example on how to use, integrate and customise the cast feature with the Dailymotion Player on an Android environment.


Prerequisites


1. Add Cast SDKs dependencies

Add Dailymotion Cast SDK and Google Cast SDK dependencies in your build.gradle:

dependencies {
    ...
    implementation 'com.dailymotion.player.android:cast:1.2.7'
    implementation 'androidx.mediarouter:mediarouter:1.6.0'
    implementation 'com.google.android.gms:play-services-cast-framework:21.4.0'
    ...
}

In addition to those dependencies, you may need to include additional dependencies needed by the Google Cast SDK. Please check Google Cast documentation to make sure to include them all.

2. Create an expanded controller

To improve the casting experience, create an expanded controller using Google Cast SDK's widget ExpandedControlsActivity.

Refer to Google Cast SDK documentation  to see this step in more details.

You can create a simple ExpandedControlsActivity as shown below:

package com.dailymotion.sample.player.sdk.cast
 
import com.google.android.gms.cast.framework.media.widget.ExpandedControllerActivity
import android.view.Menu
import com.dailymotion.sample.player.sdk.R
import com.google.android.gms.cast.framework.CastButtonFactory
 
/**
 * An example of extending [ExpandedControllerActivity] to add a cast button.
 */
class ExpandedControlsActivity : ExpandedControllerActivity() {
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        super.onCreateOptionsMenu(menu)
        menuInflater.inflate(R.menu.menu_cast_expanded_controller, menu)
        CastButtonFactory.setUpMediaRouteButton(this, menu, R.id.media_route_menu_item)
        return true
    }
}

With menu_cast_expanded_controller being:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
 
    <item
        android:id="@+id/media_route_menu_item"
        android:orderInCategory="101"
        android:title="@string/cast_media_route_menu_title"
        app:actionProviderClass="androidx.mediarouter.app.MediaRouteActionProvider"
        app:showAsAction="always" />
 
</menu>

3. Modify your CastOptionsProvider

In your CastOptionsProvider class used to create the OptionsProvider, call DailymotionCast.castOptionsBuilder() to get a pre-built CastOptions.Builder that already includes a receiver application id.

An example CastOptionsProvider using our Dailymotion Cast SDK would look like this:

package com.dailymotion.sample.player.sdk.cast
 
import android.content.Context
import com.dailymotion.player.android.sdk.cast.DailymotionCast
import com.google.android.gms.cast.LaunchOptions
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.MediaIntentReceiver
import com.google.android.gms.cast.framework.media.NotificationOptions
 
class CastOptionsProvider : OptionsProvider {
    override fun getCastOptions(context: Context): CastOptions {
 
        val buttonActions = listOf(
            MediaIntentReceiver.ACTION_TOGGLE_PLAYBACK,
            MediaIntentReceiver.ACTION_STOP_CASTING
        )
 
        // Showing "play/pause" and "stop casting" in the compat view of the notification.
        val compatButtonActionsIndices = intArrayOf(0, 1)
 
        // Use our sample ExpandedControlsActivity
        val notificationOptions = NotificationOptions.Builder()
            .setActions(buttonActions, compatButtonActionsIndices)
            .setTargetActivityClassName(ExpandedControlsActivity::class.java.name)
            .build()
 
        // Use our sample ExpandedControlsActivity
        val mediaOptions = CastMediaOptions.Builder()
            .setNotificationOptions(notificationOptions)
            .setExpandedControllerActivityClassName(ExpandedControlsActivity::class.java.name)
            .build()
 
        val launchOptions = LaunchOptions.Builder()
            .setAndroidReceiverCompatible(true)
            .build()
 
        // Call DailymotionCast.castOptionsBuilder() to have a pre-built CastOptions.Builder
        // with an already set receiver application id
        return DailymotionCast.castOptionsBuilder()
            .setLaunchOptions(launchOptions)
            .setCastMediaOptions(mediaOptions)
            .build()
    }
 
    override fun getAdditionalSessionProviders(context: Context): MutableList<SessionProvider>? {
        return null
    }
}

4. Add a cast button

The Dailymotion Cast SDK handles communication between a casting device and the Dailymotion Player. However, in order to initiate a casting session, you need to add a cast button in the UI. 

Refer to the Google Cast SDK documentation to see how to add a Cast button .

5. Customize the UI (Optional)

If you want to customize the appearance of the cast button, mini controller and expanded controller, please refer to the Google Cast SDK documentation to customize the UI .