Chromecast
This module’s goal is to ease the integration of GoogleCast and the Akamai AMP Player for iOS. With just a few steps we can achieve this integration, let’s see how.
Installation
Just import the AmpChromecast.framework into your project. For more information check out the AmpCore’s documentation.
Note: Make sure to add the frameworks as Embedded Binaries or you’ll get an error about a missing image.
How to Use
Let’s first import the required frameworks:
import AmpCore
import AmpChromecast
Then, let’s add the related variables in our UIViewController:
var ampPlayer: AmpPlayer!
var ampChromecast: AmpChromecastManager!
And it’s time to instantiate them now in your viewDidLoad function:
ampPlayer = AmpPlayer(parentView: self.view)
ampPlayer.registerObserver(self)
ampPlayer.setLicense(license)
ampChromecast = AmpChromecastManager(ampPlayer: ampPlayer, parentViewController: self)
ampPlayer.play(url: url)
You will also need to send a new object with the media info of the active playback.
let builder = GCKMediaInformationBuilder()
builder.contentID = "https://sample-url/video.m3u8"
builder.streamType = .buffered
builder.contentType = "application/x-mpegURL"
ampChromecast.mediaInfo = builder.build()
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.
Chromecast Configuration.
Use these variables and methods to configure AMP Chromecast.
Variables
Variable | Type | Description | Optional |
---|---|---|---|
isActive |
Bool | Whether or not casting is active | No |
ampPlayer |
AmpPlayer | AMP Player instance | Yes |
sessionManager |
GCKSessionManager | Session Manager | Yes |
defaultUrl |
String | Default media resource playback URL | No |
defaultContentType |
String | Media resource content type | No |
Methods
Methods | Parameters | Usage | Returns |
---|---|---|---|
registerObserver |
observer: AmpChromecastObserver | Registers an observer | The id of the registered observer |
removeObserver |
id: Int | Removes a registered observer. | |
initializeContext |
receiverId: String, loggerDelegate: GCKLoggerDelegate? = nil | Initialization of the context | |
disconnect |
stopCasting: Bool = true | Deallocation of the resource | |
Playback
These are the playback related variables
Variables
Variable | Type | Description | Optional |
---|---|---|---|
playbackState |
PlaybackState | The current state of the video playback | No |
Action Sheet
The following properties can be modified to configure the Action Sheet.
Variables
Variable | Type | Description | Optional |
---|---|---|---|
actionSheetTitle |
String | Action Sheet Title. Defaults to "AMP Chromecast" | No |
actionSheetMessage |
String | Action Sheet Message. Defaults to an empty string | No |
actionSheetPlayMessage |
String | Action Sheet Play Message. Defaults to "Play Now" | No |
actionSheetQueueMessage |
String | Action Sheet Queue Message. Defaults to "Add to Queue" | No |
actionSheetCancelMessage |
String | Action Sheet Cancel Message. Defaults to "Cancel" | No |
Queue management
The Chromecast queue can also be managed through several methods provided to the user by using the following methods.
Variables
Variable | Type | Description | Optional |
---|---|---|---|
isChromecastQueueEnabled |
Bool | Whether or not the Chromecast queue is enabled | No |
Methods
Methods | Parameters | Usage | Returns |
---|---|---|---|
queueInsert |
item: GCKMediaQueueItem, beforeItemWithId:UInt = kGCKMediaQueueInvalidItemID | Insert a single item in the Chromecast queue. You can also define at which point in the queue the insertion is made. Note: enable the chromecast autoplay flag if you want the videos to show up after the current one is done. | |
queueInsert |
GCKMediaQueueItem], beforeItemWithID:UInt = kGCKMediaQueueInvalidItemID | Insert a list of media items in the Chromecast queue. You can also define at which point in the queue the insertion is made. Note: enable the chromecast autoplay flag if you want the videos to show up after the current one is done. | |
queueInsertAndPlay |
item:GCKMediaQueueItem, beforeItemWithID:UInt = kGCKMediaQueueInvalidItemID | Inserts a new element to the queue and sets it as the current playing item. | |
queueReorderItems |
withIds:[NSNumber], insertBeforeItemWithID:UInt | Reorders the list of media items in the queue. | |
queueItemCount |
Use this to check the number of items in the playback queue. | Int | |
queueRemoveItem |
withId:Int | Removes the specified item from the queue. | |
queueRemoveItem |
withIds:[NSNumber] | Removes a list of media items from the queue. If the queue becomes empty as a result, the current media session will be terminated. | |
queueItem |
at: Int | Returns the item at the specified index in the playback queue. | GCKMediaQueueItem |
queueItem |
withItemID:Int | Returns the item with the given item ID in the playback queue. | GCKMediaQueueItem |
queueIndex |
forItemId: Int | Returns the index of the item with the given item ID in the playback queue. Note: This returns-1 if there is no such item in the queue. | Int |
queueNext |
Starts the playback of the next item in the queue. | ||
queuePrevious |
Starts the playback of the previous item in the queue. | ||
Player Event Observer
List of the observable events by the Chromecast Player Event Observer.
Event | Parameters | Description |
---|---|---|
willHandleUrl |
ampPlayer: AmpPlayer | |
onBufferingStateChanged |
ampPlayer: AmpPlayer | When the buffering state has changed |
willStop |
ampPlayer: AmpPlayer | |
External Playback Delegate
This is the list of the External Playback Delegate Methods
Methods | Parameters | Usage | Returns |
---|---|---|---|
externalPlaybackState |
Returns the playback state | PlaybackState | |
play |
Starts the playback | ||
pause |
Pauses playback | ||
seekTo |
seconds: TimeInterval | Seeks to a specified point in the playback timeline | |
currentTime |
Returns the current playback time | The current playback time | |
updateObservers |
seconds: Double | Updates the observers | |
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)