Enable casting to Chromecast devices

Learn how to enable casting with the iOS SDK.


The Google Cast framework enables a viewer to stream video and audio content to a compatible TV or sound system. By enabling the Google Cast framework in your app, a viewer can use a cast button to stream your content to a Chromecast-enabled device on a shared network connection.

👍

IMPORTANT

• The JW Player iOS SDK supports casting to the Default Media Receiver and to Styled Media Receivers.

• Custom receivers are not officially supported. However, if the video playback implements the same interface used in the Default Media Receiver, you may be able to initiate a casting session with a custom receiver.

• To specify a receiver and start scanning for devices, pass a media receiver app ID to the withAppId argument of the startScanningForDevices method of the JWCastController.



Requirement

ItemDescription
Google Cast SDKThe iOS SDK handles the discovery of, casting to, and disconnecting from devices. Casting features will not appear if the dynamic build of the Google Cast SDK is not linked to your application.

1. Download the framework.
2. Manually add the framework to your project.
3. Initialize the Google Cast SDK in your AppDelegate.


Set up permissions

  1. If you are using Xcode 12 and targeting iOS 12+, enable Access Wifi Information.

    • Click the app target > Capabilities > Access WiFi Information.
    • Click the toggle to ON to enable Access Wifi Information.

  2. (Guest mode only) In the Info.plist for your app, add NSBluetoothAlwaysUsageDescription to create a Bluetooth access request. If you are using the google-cast-sdk-no-bluetooth dependency, you can skip this step.
  3. (iOS 14) In the info.plist, add the following two keys:
    NSBonjourServices with _googlecast._tcp and _<your-app-id>._googlecast._tcp inside the array
    NSLocalNetworkUsageDescription
<key>NSBonjourServices</key>
<array>
  <string>_googlecast._tcp</string>
  <string>_ABCD1234._googlecast._tcp</string>
</array>
<key>NSLocalNetworkUsageDescription</key>
<string>${PRODUCT_NAME} uses the local network to discover Cast-enabled devices on your WiFi
network.</string>


Configure and enable casting

  1. Initialize the CastContext as described in Google's article, Integrate Cast Into Your iOS App.

  2. In your app, create a JWCastController object and set its delegate. The delegate must adhere to the JWCastDelegate protocol and implement its delegate methods.
func setUpCastController() {
    castController = JWCastController(player: player)
 
    castController?.delegate = self
}
- (void)setUpCastController
{
    self.castController = [[JWCastController alloc] initWithPlayer:self.player];
    
    self.castController.delegate = self;
}
  1. Call startDiscovery() to scan for devices.

    Once available devices are discovered, the event is reported to the JWCastDelegate method, devicesAvailable:. This method supplies a list of available devices within range as an array of JWCastingDevice.
func beginScanning() {
    castController.startDiscovery()
}
- (void) beginScanning
{
    [self.castController startDiscovery];
}
func castController(_ controller: JWCastController, devicesAvailable devices: [JWCastingDevice]) 
{
    availableDevices = devices
}
-   (void)castController:(JWCastController *)controller devicesAvailable: (NSArray<JWCastingDevice *> *)devices
{
    self.availableDevices = devices;
}
  1. Call the connectToDevice: method to connect to a device.

    When a connection is established, the JWCastingDelegate method, connectedTo:, is called. This signals the ability to cast the video that is reproduced by the JWPlayer instance.
func onUserSelectedDevice(_ index: Int!) 
{
    let chosenDevice = availableDevices[index]
    castController?.connectToDevice(chosenDevice)
}
-(void)onUserSelectedDevice:(NSInteger)index
{
    JWCastingDevice *chosenDevice = availableDevices[index];
    [self.castController connectToDevice:chosenDevice];
}
  1. Call cast() on the JWCastController instance.
func castController(_ controller: JWCastController, connectedTo device: JWCastingDevice) {
    self.castController.cast()
}
-   (void)castController:(JWCastController *)controller connectedTo:(JWCastingDevice *)device
{
    [self.castController cast];
}

The JWPlayer API controls the playback of the video being casted. The JWPlayerDelegate provides the playback callbacks while casting.



FAQs

Are there changes in iOS 13 that may impact my app?
Which features are not supported when casting with an iOS SDK player?



Did this page help you?