Reconnection with Cast SDK v2

Your sender application may become implicitly disconnected from an active Cast session due to a network problem or any number of reasons beyond the application's control. When this happens, the sender should reconnect automatically to the active Cast session so that the user can continue to control the playback.

However, there may be times when you do not want reconnection to happen, such as when the same app is playing a separate media stream on the sender device, because reconnection to the Cast session would interrupt that local playback. Of course, there are nuances that you need to consider for when your app does or does not reconnect to an active Cast session under any of the various scenarios.

This guide describes how to provide the best user experience when your app becomes disconnected. It describes the reconnection states at a high level, and illustrates the callbacks issued for connection/reconnection during the application's life cycle.

Reconnection states

The following sections provide an overview of the reconnection states your app should assume and the actions it should take.

Prepare for reconnection

Certain data is required for deciding if a reconnection should be attempted, and for performing the reconnection if needed:

  • Wifi network's SSID
  • Route ID
  • Session ID
  • Media stream end time

Persist this data as soon as playback starts or as soon as the app joins a session where media is playing or paused. Once playback is finished, clear the persisted data.

Wake and reconnect

To implement the reconnection logic, your application needs to start and manage a long-running background service. Since the application can be killed by the system, you need this service to handle the reconnection logic. The application should start this service when media playback starts (or when the application joins a session that has running media). This service should terminate itself when the media end time elapses. The application should also remove this service when media playback ends.

Restore the session

The following diagram demonstrates how to restore the session with the appropriate media route in Android. Although the programatic artifacts discussed here pertain to Android applications, the reconnection logic may be applied to apps running on other platforms.

Reconnection callbacks

The diagrams below describe the callbacks issued for the connection/reconnection states during an application's life cycle.

Android reconnection callbacks

iOS reconnection callbacks