logo
SDKs
IntroductionHow to update an SDK?THEOplayer 5.0 - Native Mobile iOS/tvOS and Android SDKs
Web
Getting started on Web (Extended)Getting started with the new Web UIGetting started on WebAPI examplesHow to implement Keyboard HotkeysHow to embed an iframeHow to implement a seamless transition between videos?How can I use video inside THEOplayer?How to work around browser cache with a new license?How to customise quality selection & labels (MP4)How to get frame-accurate currentTime display in the UI Control barHow to play an LCEVC source with THEOplayerHow to use WebXR with THEOplayer
Android legacy (4.12.x)
Getting started on Legacy Android SDK (4.12.x)Legacy Android SDK (4.12.x) customizationHow to couple the native MediaRouteButton to THEOplayerHow to enable -experimental- native rendering on AndroidHow to do offline Playback with AES-128 Encrypted Streams on Android
Android
Getting started with the Android UIGetting started on AndroidAndroid Feature IntegrationsMigration from THEOplayer Android/AndroidTV/FireTV SDK 4.x to THEOplayer Android SDK 5.0
iOS legacy (4.12.x)
Getting started on iOSiOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x) CustomizationMy app does not want to build for the app storeHow to bypass copy() not working in Safari consoleHow to couple the native GCKUICastButton to THEOplayeriOS/tvOS SDK and Legacy iOS/tvOS SDK (4.12.x) Touch-events (gestures)Building for iOS Simulator, but the linked and embedded framework THEOplayerSDK.framework was built for iOS + iOS SimulatorHow to implement custom local network access (LNA) interstitial dialog for Chromecast
iOS
Getting started guide for THEOplayer iOS/tvOS SDK 5.0THEOplayer iOS/tvOS 5.0 Feature IntegrationsMigration from THEOplayer iOS and tvOS SDK 4.x to THEOplayer iOS/tvOS SDK 5.0
Android TV Legacy (4.12.x)
Getting started on Android TV Legacy (4.12.x)
tvOS
Getting started on tvOS
Chromecast
Getting started on ChromecastChromecast Application Customization
Webos
Getting Started on webOS
Tizen
Getting started on TizenInstalling the Tizen developer toolsSetting up a Tizen device for debuggingDeploying a test app on a physical Tizen deviceDeploying a test app on a Tizen emulator
Roku
Getting Started on Roku
Fire TV Legacy (4.12.x)
Getting started on Fire TV Legacy (4.12.x)
Frameworks

How to implement custom local network access (LNA) interstitial dialog for Chromecast

Table of Contents

Prerequisites

THEOplayer is built in a modular way, and a THEOplayer library has a set of features enabled (e.g. HLS playback, MPEG-DASH playback, advertisements, etc.).

  1. The Chromecast feature needs to be enabled in your library. Please reach out to your THEOplayer contact to ensure that this feature is enabled.

Setup Chromecast SDK

Please follow our guide on how to setup the Google Cast SDK in your application when using THEOplayer.

Implement custom LNA screen

We can create a simple ViewController that will present our custom screen about LNA permission for the user.

import Foundation
import GoogleCast

class CustomChromecastLNAScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .green

        let introText: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 800))

        introText.text =

            "This is the first time you use Chromecast!\nAccept the permission popup and enjoy!\n\n(Pull down to close)"

        introText.numberOfLines = 5
        introText.textColor = .black
        introText.lineBreakStrategy = .pushOut
        introText.lineBreakMode = .byWordWrapping
        introText.adjustsFontSizeToFitWidth = true

        view.addSubview(introText)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    override func viewDidDisappear(_ animated: Bool) {
    // When the user close the ViewController, we trigger the discovery. This will trigger the Apple LNA popup too.
        GCKCastContext.sharedInstance().discoveryManager.startDiscovery()
        super.viewDidDisappear(animated)
    }

}

Be aware once you opt-in for a custom LNA screen the Cast SDK will not do anything anymore regarding device discovery and device selection. (These are all part of the default LNA logic from Google)

So the device discovery needs to be started manually by calling GCKCastContext.sharedInstance().discoveryManager.startDiscovery()

Use custom LNA screen

To present the new LNA screen we have 2 options:

  • connect it to a native Cast button
  • connect it to the in-player Cast button inside THEOplayer UI

On a ViewController where we have access to THEOplayer we can do something like this:

// Set up ChromeCast Button

self.castButton = GCKUICastButton(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(24), height: CGFloat(24)))
self.castButton!.tintColor = UIColor.white
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.castButton!)

self.castButton?.delegate = self // native button

self.theoplayer?.cast?.chromecast?.setCastButtonDelegate(self) // in-player button

As you can see self.castButton?.delegate is used for the native Cast button and self.theoplayer?.cast?.chromecast?.setCastButtonDelegate() is a THEOplayer API that allows you to connect custom logic for the in-player Cast button.

Both methods request a GCKUICastButtonDelegate (API)

If you implement the delegate on the ViewController itself you will have something similar:

func castButtonDidTap(toPresentLocalNetworkAccessPermissionDialog castButton: GCKUICastButton) {
    self.navigationController?.present(CustomChromecastLNAScreen(), animated: true, completion: nil)
}


func castButtonDidTap(_ castButton: GCKUICastButton, toPresentDialogFor castState: GCKCastState) {
    GCKCastContext.sharedInstance().presentCastDialog()
}

The first method is called when you have to present your LNA screen, the second method is called when you can show the device selection screen.

That's all!

Now your custom logic will be triggered whenever the user clicks on the native or on the in-player Chromecast button!

github
Make sure to follow us on GitHub!
THEO-logo-white
twitter
facebook
linkedin
Copyright © 2022. All Rights Reserved.
Leuven
New York
Singapore
Barcelona