Google Cast iOS(Swift) Sender App

iOS sender App for Google Cast MediaControl Overview

Jay Savsani
Mac O’Clock

--

This is the second article in a series of 3 where I’ll show you how to add media control while your iOS app is having an active cast session. You can read my previous article about basic integration for iOS sender App of Google cast here. I’m assuming that you’ve gone through the previous article and have bare minimum code that can cast a video to Chrome Cast. Now that we are able to Cast Video, how we can show Big and Small media controller inside our App like Netflix does(adding screenshot below).

Expanded Media Controller

It’s super easy to open Expanded media controller in sender App. Google Cast provides a simple method that you can use to open this controller. Add below code after your line where you’ve loaded your cast request.

GCKCastContext.sharedInstance().presentDefaultExpandedMediaControls()

So your code block will look something like this after you’ve added media controller.

Now, cast any video and you’ll be able to see Expanded media controller like this screenshot. It’s a default controller provided by Google Cast. You can customise this controller as per your requirement by referring to the official documentation.

Mini Media Controller

As per the design checklist by Google Cast, a sender app should provide a persistent control known as the mini controller that should appear when the user navigates away. However, it completely depends on your priority/choice where you want to show it or hide it. There's two way how we can add this mini media controller.

  • Option 1: Let the Cast framework manage the layout of the mini controller by wrapping your existing view controller with its own view controller. For achieving this you need to attach a mini media view controller to your Navigation Controller.
  • Option 2: Manage the layout of the mini controller widget yourself by adding it to your existing view controller by providing a subview in the storyboard. By choosing this option you’re free to add mini media controller to any of your existing view controller by adding it as subview.

Option 1: Wrapping up into existing view controller

Inside your app delegate, get instance your navigation controller (not view controller) and let Google Cast SDK take over all things for attaching mini media controller.


let appStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = appStoryboard.instantiateViewController(withIdentifier: "MainNavigation")
let castContainerVC = GCKCastContext.sharedInstance().createCastContainerController(for: navigationController)
castContainerVC.miniMediaControlsItemEnabled = true
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = castContainerVC
window!.makeKeyAndVisible()

Now, this code looks really simple and easy but what if you don’t want to add mini media to your main navigation. What if you’ve multiple storyboard and your navigation controller are different. What if you’ve TabView. This flow is bit complicated and you’d certainly not like to add it directly to your main navigation.

Options 2: Add as a Subview

You can also add a mini media controller as a subview inside any view controller, which you’d find more elegant compare to option 1. create two variable inside your view controller as below

private var miniMediaControlsViewController: GCKUIMiniMediaControlsViewController! var mediaView: UIView!

and inside viewDidLoad add this mediaView programmatically

mediaView = UIView(frame: CGRect(x: 0, y: self.view.frame.height - 70 - (UIApplication.shared.keyWindow?.safeAreaInsets.bottom)!, width: self.view.frame.width, height: 70))self.view.addSubview(mediaView!)

Now time to create mini media view controller and adding it to an existing view controller and install it as a container view into an existing view controller

let castContext = GCKCastContext.sharedInstance()sessionManager.add(self)miniMediaControlsViewController = castContext.createMiniMediaControlsViewController()
miniMediaControlsViewController.delegate = self
installViewController(miniMediaControlsViewController, inContainerView: mediaView!)func installViewController(_ viewController: UIViewController?, inContainerView containerView: UIView) { if let viewController = viewController { addChild(viewController)
viewController.view.frame = containerView.bounds
containerView.addSubview(viewController.view)
viewController.didMove(toParent: self)
}}

Now your code block would look something like below screenshot

You can control the visibility of mini-media controller base on your need. updateControlBarVisibility method is used to maintain visibility of the controller. Now run the app and start casting. It’ll give you mini media like below screenshot.

Okay !! So now your app is fully qualified as per Google Cast Checklist of sender app. You can customise the UI of the controller as per your need exploring Google Cast SDK.

I’m concluding this article here and adding Github link of this project. You can clone it and play around it via modifying code.

--

--