How do I ensure Airplay keeps playing when I lock my phone?

When I Airplay a video from my phone to Safari or to AppleTV and then lock my phone, the video stops playing. How do I ensure playback resumes even though I lock my phone?

Hey Callum, thanks for asking this here. This is a question that we often get from our customers. Normally the BasicPlayback sample application from Bitmovin is starting point for most customers to start playing around with Bitmovin iOS player SDK. This sample application allows Airplay use case but is not configured to continue Airplay when the application is put to background or phone is locked.

To achieve this, the application needs to be configured to continue playback in background mode. This cane be done by following below 2 steps.

  • Step1 : Set AVAudioSessionCategoryPlayback category on the audio session. You can find reference code for this in BackgroundPlayback sample app. Copying code below for quick reference.
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Make sure that the correct audio session category is set to allow for background playback.
        handleAudioSessionCategorySetting()
    }

    /* Set AVAudioSessionCategoryPlayback category on the audio session. This category indicates that audio playback
    is a central feature of your app. When you specify this category, your app’s audio continues with the Ring/Silent
    switch set to silent mode (iOS only). With this category, your app can also play background audio if you're
    using the Audio, AirPlay, and Picture in Picture background mode. To enable this mode, under the Capabilities
    tab in your XCode project, set the Background Modes switch to ON and select the “Audio, AirPlay, and Picture in
    Picture” option under the list of available modes. */
    func handleAudioSessionCategorySetting() {
        let audioSession = AVAudioSession.sharedInstance()

        // When AVAudioSessionCategoryPlayback is already active, we have nothing to do here
        guard audioSession.category.rawValue != AVAudioSession.Category.playback.rawValue else { return }

        do {
            try audioSession.setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.moviePlayback)
        } catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }
    }
  • Step 2 : Under the Capabilities tab in your XCode project, set the Background Modes switch to ON and select the “Audio, AirPlay, and Picture in Picture” option under the list of available modes like shown in screenshot below.

The other cases where playback may stop on Airplay device is if Bitmovin player instance is destroyed or if the application itself is destroyed. You can refer to BackgroundPlayback sample application from Bitmovin for above settings as well as for sample code on how to control playback from lock screen.

2 Likes