0

I am developing an iOS app in Swift that plays audio files on TV over Google Cast.

App uses two-way communication over TextChannel to exchange information with the receiver app.

The app is working as expected until I put the app into the background. After the app is backgrounded the Google Cast session gets disconnected.

To ensure app is not susspended in the background while playing audio I have in my AppDelegate this code snippet:

try session.setCategory(AVAudioSession.Category.playback,
                                mode: AVAudioSession.Mode.default,
                                options: [AVAudioSession.CategoryOptions.mixWithOthers, AVAudioSession.CategoryOptions.allowAirPlay])
        try session.setActive(true)

This is how I initialise the Google Cast:

let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID)
    let options = GCKCastOptions(discoveryCriteria: criteria)
    options.suspendSessionsWhenBackgrounded = false
    GCKCastContext.setSharedInstanceWith(options)
    GCKCastContext.setSharedInstanceWith(options)

If I add a dummy AVPlayer to my ViewController and play a random mp3 file on the player while I play other audio files overcast connection to the Google Cast session remains connected and the app works as expected until mp3 stops playing on AVPlayer.

Here is my AVPlayer snippet:

let videoURL = URL(string: "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3")
        let player = AVPlayer(url: videoURL!)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        player.play()

Can anybody help me prevent the app from being suspended when it plays audio only while casting to TV?

Thanks!

Kirollos Mallak
  • 359
  • 4
  • 12
Marko Ilic
  • 11
  • 1
  • Isn't communication with receiver app is performed through Google Cast SDK? What is TextChannel? I think you should check you receiver app implementation why it's stopping when sender goes to background – Cy-4AH Jan 17 '22 at 15:25
  • I think the app's audio background mode is set up fine, it could be due to the disconnection / closing of the Socket connection when your app goes into the background. Maybe this helps: https://stackoverflow.com/questions/22011821/chromecast-background-video-playback-support-ios – Shawn Frank Jan 17 '22 at 15:30

0 Answers0