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, set a media receiver app ID to the chromeCastReceiverAppID property of the JWCastController.

• Avoid calling the cast() method inside the onConnected delegate callback method, since it takes a finite amount of time for processes related to the established connection to resolve. If unavoidable, delaying the cast() call can be effective (such as using a Dispatch queue, for example).

📘

• The JW Player iOS SDK does not maintain support for the Google Cast iOS SDK's UI elements.

• For example, rather than using a GCKUICastButton, you will need to implement your own cast button that calls JWP iOS SDK methods.

• An example of the above can be found in our Best Practice App in both Objective-C and Swift.


Add the dependency

  1. In a text editor, open Podfile.
  2. If you are using CocoaPods to manage dependencies, add google-cast-sdk (guest mode support) to your Podfile, as shown in the following code example. Be sure to use a Google Cast version that is compatible with your version of the iOS SDK and the proper Podfile syntax.

    As of iOS 13, instead of adding the google-cast-sdk dependency, you can add the google-cast-sdk-no-bluetooth (no guest mode support) non-bluetooth dependency to your Podfile. However, we strongly recommend using google-cast-sdk.

    If you are manually managing dependencies for your project, follow these manual setup instructions.
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'

target 'MyAwesomeProject' do
    # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
    use_frameworks!

    # Pods for MyAwesomeProject
    pod 'JWPlayer-SDK', '3.19.1'
    pod 'google-cast-sdk', '4.4.5'
end
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'

target 'MyAwesomeProject' do
    # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
    use_frameworks!

    # Pods for MyAwesomeProject
    pod 'JWPlayer-SDK', '3.19.1'
    pod 'google-cast-sdk-no-bluetooth', '4.4.5' 
end


Configure and enable casting

  1. If you are using Xcode 10 and targeting iOS 12+, 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. In your app, create a JWCastController object and set its delegate. The delegate must adhere to the JWCastingDelegate protocol and implement its delegate methods.
- (void)setUpCastController
{
    self.castController = [[JWCastController alloc] initWithPlayer:self.player];
    self.castController.chromeCastReceiverAppID = kGCKDefaultMediaReceiverApplicationID;
    self.castController.delegate = self;
}
func setUpCastController() {
    castController = JWCastController(player: player)
    castController?.chromeCastReceiverAppID = kGCKDefaultMediaReceiverApplicationID
    castController?.delegate = self
}
  1. Call the scanForDevices method to scan for devices: [self.castController scanForDevices];. When devices become available, the JWCastingDelegate method, onCastingDevicesAvailable:, is called and returns an array of JWCastingDevices.
-(void)onCastingDevicesAvailable:(NSArray *)devices
{
    self.availableDevices = devices;
}
func onCastingDevicesAvailable(_ devices: [JWCastingDevice]!) 
{
    availableDevices = devices
}
  1. Call the connectToDevice: method to connect to a device. When a connection is established, the JWCastingDelegate method, onConnectedToCastingDevice:, is called. This signals the ability to cast the video that is reproduced by the JWPlayerController.
-(void)onUserSelectedDevice:(NSInteger)index
{
    JWCastingDevice *chosenDevice = availableDevices[index];
    [self.castController connectToDevice:chosenDevice];
}
func onUserSelectedDevice(_ index: Int!) 
{
    let chosenDevice = availableDevices[index]
    castController?.connect(to: chosenDevice)
}
  1. Call the cast method on your JWCastController.
-(void)onConnectedToCastingDevice:(JWCastingDevice *)device
{
    [self.castController cast];
}
func onConnected(to device: JWCastingDevice?) {
    castController?.cast()
}

The JWPlayerController API controls the playback of the video being casted, and the JWPlayerDelegate will provide you with the playback callbacks while casting.


📘

TIP

The JW Player iOS Best Practice Apps repository has an example casting implementation. Additionally, you can learn more about the Google Cast User Experience guidelines.



FAQs

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



Did this page help you?