Add Advanced Features to Your iOS App

Ad Breaks

The iOS Sender SDK provides support for Ad Breaks and companion ads within a given media stream.

See the Web Receiver Ad Breaks Overview for more information on how Ad Breaks work.

While breaks can be specified on both the sender and receiver, it is recommended for them to be specified on the Web Receiver and Android TV Receiver to maintain consistent behavior across platforms.

On iOS, specify Ad Breaks in a load command using GCKAdBreakClipInfo and GCKAdBreakInfo :

Swift
 let 
  
 breakClip1Builder 
  
 = 
  
 GCKAdBreakClipInfoBuilder 
 ( 
 adBreakClipID 
 : 
  
 "bc0" 
 ) 
 breakClip1Builder 
 . 
 title 
  
 = 
  
 "Clip title" 
 if 
  
 let 
  
 posterUrl 
  
 = 
  
 URL 
 ( 
 string 
 : 
  
 "https://www.some.url" 
 ) 
  
 { 
  
 breakClip1Builder 
 . 
 posterURL 
  
 = 
  
 posterUrl 
 } 
 breakClip1Builder 
 . 
 duration 
  
 = 
  
 60 
 breakClip1Builder 
 . 
 whenSkippable 
  
 = 
  
 5 
  
 // Set this field so that the ad is skippable 
 let 
  
 breakClip1 
  
 = 
  
 breakClip1Builder 
 . 
 build 
 () 
 let 
  
 breakClip2 
  
 = 
  
 ... 
 let 
  
 breakClip3 
  
 = 
  
 ... 
 let 
  
 break1 
  
 = 
  
 GCKAdBreakInfoBuilder 
 ( 
 adBreakID 
 : 
  
 "b0" 
 , 
  
 adBreakClipIds 
 : 
  
 [ 
 "bc0" 
 , 
  
 "bc1" 
 , 
  
 "bc2" 
 ]). 
 build 
 () 
 let 
  
 mediaInfoBuilder 
  
 = 
  
 GCKMediaInformationBuilder 
 ( 
 entity 
 : 
  
 "entity" 
 ) 
 ... 
 mediaInfoBuilder 
 . 
 adBreaks 
  
 = 
  
 [ 
 break1 
 ] 
 mediaInfoBuilder 
 . 
 adBreakClips 
  
 = 
  
 [ 
 breakClip1 
 , 
  
 breakClip2 
 , 
  
 breakClip3 
 ] 
 ... 
 mediaInformation 
  
 = 
  
 mediaInfoBuilder 
 . 
 build 
 () 
 let 
  
 mediaLoadRequestDataBuilder 
  
 = 
  
 GCKMediaLoadRequestDataBuilder 
 () 
 mediaLoadRequestDataBuilder 
 . 
 mediaInformation 
  
 = 
  
 mediaInformation 
 sessionManager 
 . 
 currentSession 
 ?. 
 remoteMediaClient 
 ?. 
 loadMedia 
 ( 
 with 
 : 
  
 mediaLoadRequestDataBuilder 
 . 
 build 
 ()) 
Objective-C
 GCKAdBreakClipInfoBuilder 
  
 * 
 breakClipInfoBuilder 
  
 = 
  
 [[ 
 GCKAdBreakClipInfoBuilder 
  
 alloc 
 ] 
  
 initWithAdBreakClipID 
 : 
 @"bc0" 
 ]; 
 breakClipInfoBuilder 
 . 
 title 
  
 = 
  
 @"Clip title" 
 ; 
 breakClipInfoBuilder 
 . 
 posterURL 
  
 = 
  
 [[ 
 NSURL 
  
 alloc 
 ] 
  
 initWithString 
 : 
 @"https://www.some.url" 
 ]; 
 breakClipInfoBuilder 
 . 
 duration 
  
 = 
  
 60 
 ; 
 breakClipInfoBuilder 
 . 
 whenSkippable 
  
 = 
  
 5 
 ; 
 GCKAdBreakClipInfo 
  
 * 
 breakClip1 
  
 = 
  
 breakClipInfoBuilder 
 . 
 build 
 ; 
 GCKAdBreakClipInfo 
  
 * 
 breakClip2 
  
 = 
  
 ... 
 GCKAdBreakClipInfo 
  
 * 
 breakClip3 
  
 = 
  
 ... 
 GCKAdBreakInfo 
  
 * 
 break1 
  
 = 
  
 [[ 
 GCKAdBreakInfoBuilder 
  
 alloc 
 ] 
  
 initWithAdBreakID 
 : 
 @"b0" 
  
 adBreakClipIds 
 : 
 @[ 
 @"bc0" 
 , 
  
 @"bc1" 
 , 
  
 @"bc2" 
 ] 
 ]. 
 build 
 ; 
 GCKMediaInformationBuilder 
  
 * 
 mediaInfoBuilder 
  
 = 
  
 [[ 
 GCKMediaInformationBuilder 
  
 alloc 
 ] 
  
 initWithEntity 
 : 
 @"entity" 
 ]; 
 ... 
 mediaInfoBuilder 
 . 
 adBreaks 
  
 = 
  
 @[ 
 break1 
 ] 
 ; 
 mediaInfoBuilder 
 . 
 adBreakClips 
  
 = 
  
 @[ 
 breakClip1 
 , 
  
 breakClip2 
 , 
  
 breakClip3 
 ] 
 ; 
 ... 
 self 
 . 
 mediaInformation 
  
 = 
  
 [ 
 mediaInfoBuilder 
  
 build 
 ]; 
 GCKMediaLoadRequestDataBuilder 
  
 * 
 mediaLoadRequestDataBuilder 
  
 = 
  
 [[ 
 GCKMediaLoadRequestDataBuilder 
  
 alloc 
 ] 
  
 init 
 ]; 
 mediaLoadRequestDataBuilder 
 . 
 mediaInformation 
  
 = 
  
 self 
 . 
 mediaInformation 
 ; 
 // Send a load request to the remote media client. 
 GCKRequest 
  
 * 
 request 
  
 = 
  
 [ 
 self 
 . 
 sessionManager 
 . 
 currentSession 
 . 
 remoteMediaClient 
  
 loadMediaWithLoadRequestData 
 :[ 
 mediaLoadRequestDataBuilder 
  
 build 
 ]]; 

Variable playback rate

Your app can display and change the playback rate for the current media item. You are able to set the rate using -[setPlaybackRate:] or -[setPlaybackRate:customData:] of the GCKRemoteMediaClient , access the GCKUIPlaybackRateController using playbackRateController of the GCKUIMediaController , and display the current playback rate using playbackRate of the GCKUIPlaybackRateController .

Sample code

The following two files implement GCKUIPlaybackRateController which controls the playback rate using a segmented control that has "normal", "half speed", and "double speed" buttons:

Swift
 import 
  
 GoogleCast 
 /** 
 * An implementation of GCKUIPlaybackRateController that controls playback rate 
 * using a segmented control that has "normal", "half speed", and "double speed" 
 * buttons. 
 */ 
 class 
  
 SegmentedButtonPlaybackRateController 
 : 
  
 GCKUIPlaybackRateController 
  
 { 
  
 static 
  
 let 
  
 kSegmentNormal 
  
 = 
  
 0 
 ; 
  
 static 
  
 let 
  
 kSegmentHalfSpeed 
  
 = 
  
 1 
 ; 
  
 static 
  
 let 
  
 kSegmentDoubleSpeed 
  
 = 
  
 2 
 ; 
  
 var 
  
 segmentedControl 
 : 
  
 UISegmentedControl 
 ! 
  
 override 
  
 var 
  
 playbackRate 
 : 
  
 Float 
  
 { 
  
 didSet 
  
 { 
  
 var 
  
 buttonIndex 
  
 = 
  
 0 
  
 // Map the playback rate to one of our three supported speeds. 
  
 if 
  
 playbackRate 
  
 == 
  
 1.0 
  
 { 
  
 buttonIndex 
  
 = 
  
 SegmentedButtonPlaybackRateController 
 . 
 kSegmentNormal 
  
 } 
  
 else 
  
 if 
  
 playbackRate 
 < 
 1.0 
  
 { 
  
 buttonIndex 
  
 = 
  
 SegmentedButtonPlaybackRateController 
 . 
 kSegmentHalfSpeed 
  
 } 
  
 else 
  
 { 
  
 buttonIndex 
  
 = 
  
 SegmentedButtonPlaybackRateController 
 . 
 kSegmentDoubleSpeed 
  
 } 
  
 segmentedControl 
 ?. 
 selectedSegmentIndex 
  
 = 
  
 buttonIndex 
  
 } 
  
 } 
  
 override 
  
 var 
  
 inputEnabled 
 : 
  
 Bool 
  
 { 
  
 didSet 
  
 { 
  
 segmentedControl 
 ?. 
 isEnabled 
  
 = 
  
 inputEnabled 
  
 } 
  
 } 
  
 /** 
 * Designated initializer. 
 * 
 * @param segmentedControl The segmented control for changing/displaying the 
 * playback rate. 
 */ 
  
 convenience 
  
 init 
 ( 
 _ 
  
 segmentedControl 
 : 
  
 UISegmentedControl 
 ) 
  
 { 
  
 self 
 . 
 init 
 () 
  
 self 
 . 
 segmentedControl 
  
 = 
  
 segmentedControl 
 ; 
  
 segmentedControl 
 . 
 addTarget 
 ( 
 self 
 , 
  
 action 
 : 
  
 #selector 
 ( 
 segmentedControlTapped 
 ( 
 sender 
 :)), 
  
 for 
 : 
  
 UIControl 
 . 
 Event 
 . 
 valueChanged 
 ) 
  
 } 
  
 @objc 
  
 func 
  
 segmentedControlTapped 
 ( 
 sender 
 : 
  
 UISegmentedControl 
 ) 
  
 { 
  
 var 
  
 playbackRate 
 : 
  
 Float 
  
 = 
  
 1.0 
  
 switch 
  
 segmentedControl 
 ?. 
 selectedSegmentIndex 
  
 { 
  
 case 
  
 SegmentedButtonPlaybackRateController 
 . 
 kSegmentHalfSpeed 
 : 
  
 playbackRate 
  
 = 
  
 0.5 
 ; 
  
 case 
  
 SegmentedButtonPlaybackRateController 
 . 
 kSegmentDoubleSpeed 
 : 
  
 playbackRate 
  
 = 
  
 2.0 
 ; 
  
 case 
  
 SegmentedButtonPlaybackRateController 
 . 
 kSegmentNormal 
 : 
  
 fallthrough 
  
 default 
 : 
  
 playbackRate 
  
 = 
  
 1.0 
 ; 
  
 } 
  
 self 
 . 
 playbackRate 
  
 = 
  
 playbackRate 
  
 } 
 } 
Objective-C

SegmentedButtonPlaybackRateController.h

 #import <GoogleCast/GoogleCast.h 
> #import <UIKit/UIKit.h 
> /** 
 * An implementation of GCKUIPlaybackRateController that controls playback rate 
 * using a segmented control that has "normal", "half speed", and "double speed" 
 * buttons. 
 */ 
 @interface 
 SegmentedButtonPlaybackRateController 
: GCKUIPlaybackRateController 
 /** 
 * Designated initializer. 
 * 
 * @param segmentedControl The segmented control for changing/displaying the 
 * playback rate. 
 */ 
 - 
 ( 
 instancetype 
 ) 
 initWithSegmentedControl: 
 ( 
 UISegmentedControl 
  
 * 
 ) 
 segmentedControl 
 ; 
 @end 

SegmentedButtonPlaybackRateController.m

 #import "SegmentedButtonPlaybackRateController.h" 
 @interface 
 SegmentedButtonPlaybackRateController 
  
 () 
  
 { 
  
 UISegmentedControl 
  
 * 
 _segmentedControl 
 ; 
 } 
 @end 
 static 
  
 const 
  
 NSInteger 
  
 kSegmentNormal 
  
 = 
  
 0 
 ; 
 static 
  
 const 
  
 NSInteger 
  
 kSegmentHalfSpeed 
  
 = 
  
 1 
 ; 
 static 
  
 const 
  
 NSInteger 
  
 kSegmentDoubleSpeed 
  
 = 
  
 2 
 ; 
 @implementation 
 SegmentedButtonPlaybackRateController 
 - 
 ( 
 instancetype 
 ) 
 initWithSegmentedControl: 
 ( 
 UISegmentedControl 
  
 * 
 ) 
 segmentedControl 
  
 { 
  
 if 
  
 ( 
 self 
  
 = 
  
 [ 
 super 
  
 init 
 ]) 
  
 { 
  
 _segmentedControl 
  
 = 
  
 segmentedControl 
 ; 
  
 [ 
 _segmentedControl 
  
 addTarget 
 : 
 self 
  
 action 
 : 
 @selector 
 ( 
 segmentedControlTapped 
 : 
 ) 
  
 forControlEvents 
 : 
 UIControlEventValueChanged 
 ]; 
  
 } 
  
 return 
  
 self 
 ; 
 } 
 - 
 ( 
 void 
 ) 
 setPlaybackRate: 
 ( 
 float 
 ) 
 playbackRate 
  
 { 
  
 [ 
 super 
  
 setPlaybackRate 
 : 
 playbackRate 
 ]; 
  
 NSInteger 
  
 buttonIndex 
  
 = 
  
 0 
 ; 
  
 // Map the playback rate to one of our three supported speeds. 
  
 if 
  
 ( 
 playbackRate 
  
 == 
  
 1.0 
 ) 
  
 { 
  
 buttonIndex 
  
 = 
  
 kSegmentNormal 
 ; 
  
 } 
  
 else 
  
 if 
  
 ( 
 playbackRate 
 < 
 1.0 
 ) 
  
 { 
  
 buttonIndex 
  
 = 
  
 kSegmentHalfSpeed 
 ; 
  
 } 
  
 else 
  
 { 
  
 buttonIndex 
  
 = 
  
 kSegmentDoubleSpeed 
 ; 
  
 } 
  
 _segmentedControl 
 . 
 selectedSegmentIndex 
  
 = 
  
 buttonIndex 
 ; 
 } 
 - 
 ( 
 void 
 ) 
 setInputEnabled: 
 ( 
 BOOL 
 ) 
 inputEnabled 
  
 { 
  
 _segmentedControl 
 . 
 enabled 
  
 = 
  
 inputEnabled 
 ; 
  
 [ 
 super 
  
 setInputEnabled 
 : 
 inputEnabled 
 ]; 
 } 
 - 
 ( 
 void 
 ) 
 segmentedControlTapped: 
 ( 
 id 
 ) 
 sender 
  
 { 
  
 float 
  
 playbackRate 
 ; 
  
 switch 
  
 ( 
 _segmentedControl 
 . 
 selectedSegmentIndex 
 ) 
  
 { 
  
 case 
  
 kSegmentHalfSpeed 
 : 
  
 playbackRate 
  
 = 
  
 0.5 
 ; 
  
 break 
 ; 
  
 case 
  
 kSegmentDoubleSpeed 
 : 
  
 playbackRate 
  
 = 
  
 2.0 
 ; 
  
 break 
 ; 
  
 case 
  
 kSegmentNormal 
 : 
  
 default 
 : 
  
 playbackRate 
  
 = 
  
 1.0 
 ; 
  
 break 
 ; 
  
 } 
  
 self 
 . 
 playbackRate 
  
 = 
  
 playbackRate 
 ; 
 } 
 @end 

Add a custom channel

The Cast framework provides two ways to create a channel to send custom messages to a Web Receiver:

  1. GCKCastChannel is meant to be subclassed to implement non-trivial channels that have associated state.
  2. GCKGenericChannel is provided as an alternative to subclassing; it passes its received messages to a delegate so that they can be processed elsewhere.

Here is an example of a GCKCastChannel implementation:

Swift
 class 
  
 HGCTextChannel 
 : 
  
 GCKCastChannel 
  
 { 
  
 override 
  
 func 
  
 didReceiveTextMessage 
 ( 
 _ 
  
 message 
 : 
  
 String 
 ) 
  
 { 
  
 print 
 ( 
 "received message: 
 \( 
 message 
 ) 
 " 
 ) 
  
 } 
 } 
Objective-C

HGCTextChannel.h

 #import <GoogleCast/GCKCastChannel.h 
> @interface 
 HGCTextChannel 
: GCKCastChannel 
 @end 

HGCTextChannel.m

 #import "HGCTextChannel.h" 
 @implementation 
 HGCTextChannel 
 - 
 ( 
 void 
 ) 
 didReceiveTextMessage: 
 ( 
 NSString 
 * 
 ) 
 message 
  
 { 
  
 NSLog 
 ( 
 @"received message: %@" 
 , 
  
 message 
 ); 
 } 
 @end 

A channel can be registered at any time; if the session is not currently in a connected state, the channel will automatically become connected when the session itself is connected, provided that the channel's namespace is present in the Web Receiver app metadata's list of supported namespaces.

Each custom channel is defined by a unique namespace and must start with the prefix urn:x-cast: , for example, urn:x-cast:com.example.custom . It is possible to have multiple custom channels, each with a unique namespace. The Web Receiver app can also send and receive messages using the same namespace.

Swift
 var 
  
 error 
 : 
  
 GCKError 
 ? 
 let 
  
 textChannel 
  
 = 
  
 HGCTextChannel 
 . 
 init 
 ( 
 namespace 
 : 
  
 "urn:x-cast:com.google.cast.sample.helloworld" 
 ) 
 sessionManager 
 . 
 currentCastSession 
 ?. 
 add 
 ( 
 textChannel 
 ) 
 textChannel 
 . 
 sendTextMessage 
 ( 
 "Hello World" 
 , 
  
 error 
 : 
  
 & 
 error 
 ) 
 if 
  
 error 
  
 != 
  
 nil 
  
 { 
  
 print 
 ( 
 "Error sending text message 
 \( 
 error 
 . 
 debugDescription 
 ) 
 " 
 ) 
 } 
Objective-C
 NSError 
  
 * 
 error 
 ; 
 HGCTextChannel 
  
 * 
 textChannel 
  
 = 
  
 [[ 
 HGCTextChannel 
  
 alloc 
 ] 
  
 initWithNamespace 
 : 
 @"urn:x-cast:com.google.cast.sample.helloworld" 
 ]; 
 [ 
 sessionManager 
 . 
 currentCastSession 
  
 addChannel 
 : 
 textChannel 
 ]; 
 [ 
 textChannel 
  
 sendTextMessage 
 : 
 @"Hello World" 
  
 error 
 : 
 & 
 error 
 ]; 
 if 
  
 ( 
 error 
  
 != 
  
 nil 
 ) 
  
 { 
  
 NSLog 
 ( 
 @"Error sending text message: %@" 
 , 
  
 error 
 ); 
 } 

To provide logic that needs to execute when a particular channel becomes connected or disconnected, override the -[didConnect] and -[didDisconnect] methods if using GCKCastChannel , or provide implementations for the -[castChannelDidConnect:] and -[castChannelDidDisconnect:] methods of the GCKGenericChannelDelegate if using GCKGenericChannel .

Supporting Autoplay

See Autoplay & Queueing APIs .

Override image selection and caching

Various components of the framework (namely the Cast dialog, the mini controller, the expanded controller, and the GCKUIMediaController if so configured) will display artwork for the currently casting media. The URLs to the image artwork are typically included in the GCKMediaMetadata for the media, but the sender app may have an alternate source for the URLs.

The GCKUIImagePicker protocol defines a means for selecting an appropriate image for a given usage and desired size. It has a single method, -[getImageWithHints:fromMetadata:] , which takes a GCKUIImageHints object and a GCKMediaMetadata object as parameters, and returns a GCKImage object as a result. The framework provides a default implementation of GCKUIImagePicker which always selects the first image in the list of images in the GCKMediaMetadata object, but the app can provide an alternate implementation by setting the imagePicker property of the GCKCastContext singleton.

The GCKUIImageCache protocol also defines a means of caching images that are downloaded by the framework using HTTPS. The framework provides a default implementation of GCKUIImageCache which stores downloaded image files in the app's cache directory, but the app can provide an alternate implementation by setting the imageCache property of the GCKCastContext singleton.

Next steps

This concludes the features that you can add to your iOS Sender app. You can now build a sender app for another platform ( Android or Web ), or build a Web Receiver .

Create a Mobile Website
View Site in Mobile | Classic
Share by: