Google Cast SDK
The AmpChromecast plugin offers a quick way to integrate AmpPlayer with the Google Cast SDK for sender applications in iOS. This plugin will simplify the communication between the sender and the receiver applications by handling the sender methods.
If you desire to check out a basic implementation please refer to our AMP Chromecast application sample.
Adding the Plugin to the Xcode project
Manual Installation
To manually install the plugin you’ll need to download our latest distribution. You will be using AmpChromecast.xcframework
and the AmpCore.xcframework
to integrate this plugin.
You will also need the Google Cast SDK which you can download it on the {external-sdk-url}[release page].
The latest version of the Google Cast SDK used by this plugin is 4.8.1 . Newer versions may be used, but you may run into unexpected errors until an update is released. |
After aquiring the frameworks you can just drag them to you Xcode project directory.
The last step, is to mark every imported framework for Embed & Sign
in the project’s general tab.
CocoaPods
If you don’t have CocoaPods installed, refer to the CocoaPods documentation. To use CocoaPods, first create a Podfile in the directory containing the Xcode project. For the AmpChromecast plugin, the Podfile should look like this:
target "ProjectName" do
use_frameworks!
pod 'AmpCore'
pod 'AmpChromecast'
pod 'google-cast-sdk'
end
Once you have added the Podfile just run the command pod install --repo-update
in the directory it’s located. Once the command has been completed you should se a new .xcworkspace
file in your folder. Now you can start integrating the library.
Implementation
You need a working implementation of the AmpPlayer before using this plugin. If you need the implementation guide for AmpPlayer please refer to our documentation.
Import
The first step is to import the AmpChromecast plugin. In most scenarios you will also need the AmpCore, GoogleCast and UIKit libraries.
import AmpCore
import AmpChromecast
import GoogleCast
import UIKit
Creating a new instance
In your AppDelegate
slass, the receiver ID must be specified.
AmpChromecastManager.initializeContext(receiverId: kGCKDefaultMediaReceiverApplicationID, loggerDelegate: self)
The default receiver ID for iOS applications is kGCKMediaDefaultReceiverApplicationID for version 4.0.0 or lower; and kGCKDefaultMediaReceiverApplicationID for version 4.0.2 or higher. For custom receivers, you need to replace these values with your custom receiver ID.
|
Also in your AppDelegate
class, you need to instantiate the Main Navigation controller, create the Cast Container and enable its media controls.
let appStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = appStoryboard.instantiateViewController(withIdentifier: "MainNavigation")
let castContainerVC = GCKCastContext.sharedInstance().createCastContainerController(for: navigationController)
castContainerVC.miniMediaControlsItemEnabled = true
window!.rootViewController = castContainerVC
window!.makeKeyAndVisible()
Moving to your view controller class where casting will be managed, you need to initialize the plugin by specifying the player being used as well as the parent view controller.
let manager = AmpChromecastManager(ampPlayer: player!, parentViewController: self)
Next, you need to add the Chromecast button to your view, so make sure to add this code on your view controller viewDidLoad() function:
let castButton = GCKUICastButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: castButton)
Casting set up
You will also need to send a new object with the media info of the active playback.
let mediaInformationBuilder = GCKMediaInformationBuilder.init(contentURL: URL(string: playbackUrl)!)
mediaInformationBuilder.streamType = GCKMediaStreamType
mediaInformationBuilder.contentType = contentTyoe
For starting the video cast to a Chromecast device, just add your own button and assign it the custom class GCKUICastButton
, that way video cast will start the video cast flow automatically.
You will also be able to listen to several events from the AmpChromecastManager
by implementing AmpChromecastObserver
and assigning it to the manager as an observer. The way to achieve this is by using the ampChromecast.registerObserver(observer)
method.
Captions set up
To implement Closed Captions for a custom receiver with the AMP iOS SDK:
First, set the captions type and source.
For embedded captions, create a new track for the captions, using the GCKMediaTrack init, setting the contentIdentifier to an empty string and the contentType to “text/cea608”.
let captionsTrack = GCKMediaTrack.init(identifier: 1, contentIdentifier: "", contentType: "text/cea608", type: GCKMediaTrackType.text, textSubtype: GCKMediaTextTrackSubtype.captions, name: "English Captions", languageCode: "en", customData: nil)
For VTT captions, create a new track for the captions, using the GCKMediaTrack init, setting the contentIdentifier to the VTT captions file URL and the contentType to “text/vtt”
let captionsTrack = GCKMediaTrack.init(identifier: 1, contentIdentifier: "https://some-url/caption_en.vtt", contentType: "text/vtt", type: GCKMediaTrackType.text, textSubtype: GCKMediaTextTrackSubtype.captions, name: "English Captions", languageCode: "en", customData: nil)
Next, create an array of tracks that contains the previously created track.
let tracks = [captionsTrack]
Finally, create the mediaInfo file using the parameters shown, setting them as required by your implementation.
var mediaInfo = GCKMediaInformation(contentID: url, streamType: .buffered, contentType: "application/x-mpegURL", metadata: metadata, streamDuration: TimeInterval(100), mediaTracks:tracks, textTrackStyle:nil, customData: nil)
Listening to events
This plugin will automatically hook the events from the Chromecast SDK, and send the proper AMPPlayer Events. However, in case you want to handle yourself some of the events on Chromecast, we provide the means to do this by implementing the AmpChromecastObserver.
ampChromecast?.registerObserver(self)
extension PlayerViewController : AmpChromecastObserver {
func onChromecastPlaybackStateChanged(_ manager: AmpChromecastManager) {
print("onChromecastPlaybackStateChanged")
}
func onChromecastError(_ manager: AmpChromecastManager, error: Error?) {
print("onChromecastError \(error)")
}
func onSessionManager(_ manager: GCKSessionManager, didStart session: GCKSession) {
if let castContainerVC = UIApplication.shared.keyWindow?.rootViewController as? GCKUICastContainerViewController {
castContainerVC.miniMediaControlsItemEnabled = true
} else {
print("GCKUICastContainerViewController is not correctly configured")
}
}
func onSessionManager(_ manager: GCKSessionManager, didEnd session: GCKSession, withError error: Error?) {
if let castContainerVC = UIApplication.shared.keyWindow?.rootViewController as? GCKUICastContainerViewController {
castContainerVC.miniMediaControlsItemEnabled = false
} else {
print("GCKUICastContainerViewController is not correctly configured")
}
}
func onSessionManager(_ manager: GCKSessionManager, didFailToStart session: GCKSession, withError error: Error) {
if let castContainerVC = UIApplication.shared.keyWindow?.rootViewController as? GCKUICastContainerViewController {
castContainerVC.miniMediaControlsItemEnabled = false
} else {
print("GCKUICastContainerViewController is not correctly configured")
}
}
Additional Information
This guide was written on 5/6/2020 and it’s last revision was made on 7/16/2024. If you need any additional support please contact us through our offical support email.