Add Advanced CAF Sender Features to your iOS App

How Volume Control Works

The Cast framework automatically manages the volume for the sender app. The framework automatically synchronizes with the receiver volume for the supplied UI widgets. To sync a slider provided by the app, use GCKUIDeviceVolumeController.

Variable Playback Rate

Your app can display and change the playback rate for the current media item. Set the rate using -[setPlaybackRate:] or -[setPlaybackRate:customData:] Access the playback rate controller at playbackRateController. Display the current playback rate at 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.

FILE: 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

FILE: 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 retains the GCKCastChannel and GCKGenericChannel classes of Cast v2. The former class is meant to be subclassed to implement non-trivial channels that have associated state. The latter class is provided as an alternative to subclassing; it passes its received messages to a delegate so that they can be processed elsewhere.

In Cast v2, custom channels were enabled/disabled by registering/unregistering them with the GCKDeviceManager. That class is deprecated in CAF; channels must now be registered/unregistered with the GCKCastSession instead, using its -[addChannel:] and -[removeChannel:] methods.

The Cast framework provides two ways to create a channel to send custom messages to a 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.

Channels must be registered/unregistered with the GCKCastSession, using its -[addChannel:] and -[removeChannel:] methods.

Here is an example of a GCKCastChannel implementation:

// Header file
@interface HGCTextChannel : GCKCastChannel

@end

// Implementation file
#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 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 receiver app can also send and receive messages using the same namespace.

HGCTextChannel *textChannel =
    [[HGCTextChannel alloc] initWithNamespace:@"urn:x-cast:com.google.cast.sample.helloworld"];
[_castSession addChannel:textChannel];
[textChannel sendTextMessage:@"Hello World"];

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 GKCMediaMetadata object, but the app can provide an alternate implementation by setting the imagePicker property of the GCKCastContext singleton.

The GCKUIImageCache protocol defines a means of caching images that are downloaded by the framework via HTTPS. The framework provides oa 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 Chrome), or build a receiver app.