跳到主要内容

Chromecast

This guide shows how to set up Blandvision Player on Chromecast (Google Cast). The following guide runs through the details of set up, customization and troubleshooting tips for Chromecast. You can also check out the sample project for set up Chromecast. For basic player SDK integration, you can refer to Getting Started.

Prerequisites

We provide a CaaS (Cast as a Service) object, which not only contains setup() and cast() functions, but also contains a cast event listener, allowing you to complete this feature conveniently. To implement CaaS in your app, please follow the steps outlined in the BasicPlayback section.

Basic Usage

  1. Create a CastOptionProvider class

     /**
    * This class is needed for using Google Cast SDK, and may not be used in project code.
    */
    class CastOptionsProvider : CaaSCastOptionProvider() {
    override fun getReceiverAppId(context: Context): String {
    return "your_receiver_app_id"
    }

    // The target activity that you want to put the mini controller.
    override fun getTargetActivityClassName(): String? {
    return YourTargetActivity::class.java.name
    }
    }

    You can use CaaS default CaaSExpandedController activity.

     // The target activity that you want to put the mini controller.
    override fun getTargetActivityClassName(): String? {
    return CaaSExpandedController::class.java.name
    }

    If you don't like default activity CaaSExpandedController, just set a activity whatever you wanted but it must extend ExpandedControllerActivity

  2. Add meta-data in your AndroidManifest

     <!--This meta data is used for Google Cast framework-->
    <meta-data
    android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
    android:value="package.path.to.your.CastOptionsProvider"/>
  3. Create MediaRouteButton layout.

     <!--R.layout.button_cast-->
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.mediarouter.app.MediaRouteButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/castButton"
    android:layout_width="@dimen/btn_size"
    android:layout_height="@dimen/btn_size"
    android:layout_gravity="center_vertical"
    app:mediaRouteButtonTint="@color/white" />
  4. Inflate MediaRouteButton layout.

     val castButton = LayoutInflater.from(context).inflate(R.layout.button_cast, parnet, false) as MediaRouteButton

CaaS Setup

  1. Setup the cast button

    CaaS.setup(context, castButton)
  2. Set CastEventListener

    CaaS.setListeners(object : CastEventListener {
    override fun onConnected() {}

    override fun onDisconnected() {}

    override fun onMessageReceive(castDevice: CastDevice, namespace: String, message: String) {}

    override fun onMessageReceiveError(errorMessage: String) {}

    override fun onRemoteClientPlayerStatusUpdated(playerState: Int) {}

    override fun onStartCasting(media: MediaQueueItem) {}

    override fun onStopCasting() {}

    override fun onMetaDataChanged(metadata: CastMetaData) {}
    })
  3. Cast the video in onConnected()

    CaaS.cast(
    CastOptions(
    title = "${CONTENT_TITLE}",
    contentUrl = "${CONTENT_URL}",
    drmInfo = "${DRM_INFO_IN_MEDIA_CONFIG}",
    images = listOf(
    CastImage(
    url = "${IMAGE_URL}"
    )
    )
    )
    )
  4. Set the remote idle background image

    CaaS.setRemoteIdleImage("${IMAGE_URL}")

Mini Controller

Add CaaSMiniController fragment in your layout.

<!--
Add CaaSMiniController fragment in the layout of activity
where you want to show the mini controller.
-->
<fragment android:id="@+id/castMiniController"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone"
class="com.blendvision.player.playback.cast.presentation.CaaSMiniController"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />