Chromecast

Chromecast is a streaming media device developed by Google that allows users to wirelessly stream audio and video content from various devices to a television or display. It plugs into the HDMI port of the TV and connects to the user’s Wi-Fi network. Chromecast enables users to “cast” or send content from supported apps on their smartphones, tablets, or computers to the TV, essentially turning it into a smart TV.

We provide pre-integrations with Cast SDK for iOS. In most cases, you will only need to initialize the plugin with your Chromecast appId and optional custom data.

Cast API/SDK

The Cast SDK allows a user to select streaming audio-visual content using a Sender, and play it on (or cast it to) another device known as the Receiver, while controlling playback using the Sender.

The term Sender refers to an app that plays the role of controller for the Cast session. A Sender initiates the Cast session and manages the user’s interaction with the content.

There are many kinds of Senders, including mobile apps and Google Chrome web apps, as well as virtual control surfaces on touch-enabled Google Home devices. The media controls in the Chrome web browser function as a Sender, as does Google Assistant itself.

The term Receiver refers to an app running on a Cast-enabled device that is responsible for responding to Sender commands and for conveying streaming content from an online streaming service to the Cast-enabled device. Examples of Cast-enabled Receiver devices include Chromecasts, smart televisions, smart screens, and smart speakers.

The Cast SDK also supports multiple Senders connecting to a Cast session. For instance, one Sender could start a session on a Receiver and another Sender could join the same session to control playback, load new content, or queue more content for later.

(Source: https://developers.google.com/cast/docs/overview)

Integrate

To start with Chromecast Analytics first register a plugin to PRESTOPlay SDK:

import CastlabsChromecast

...

PRESTOPlay.shared.register([CastlabsChromecast()])

Configuration

First make sure your app meet all the configuration and requirements needed by Google (https://developers.google.com/cast/docs/ios_sender). Especially Info.plist entries for NSBonjourServices and NSLocalNetworkUsageDescription. Otherwise your application may crash.

We use no bluetooth versions of the Google Cast SDK.

let appId = "6BF5C590"
let castSettings = CastSettings(appId)
castSettings.debug = true

var cast: CastAPI = PRESTOPlaySDK.shared.cast(for: castSettings)

It is important to add a Cast Button to the view hierarchy, so user can select a cast destination device.

State

Chromecast state is reported to CastAPI.

cast?.onCastState = { [weak self] state in
    guard let self else { return }
    switch state {
    case .ready:
        if let castButton = cast?.button {
            DispatchQueue.main.async {
                castButton.center = self.view.center
                self.view.addSubview(castButton)
            }
        }
    case .sessionStart:
        if let config = playerConfig {
            self.player.pause()
            cast?.load(config)
        }
    case .sessionResume: break
    case .sessionEnd:
        self.player.play()
    case .unknown: break
    case .idle: break
    case .playing:
        print("Tracks: \(String(describing: cast?.tracks))")
    case .paused: break
    case .buffering: break
    case .loading: break
    @unknown default: break
    }
}