CarPlay audio app can't start when launched from locked device
Asked Answered
H

1

9

We have an audio app and we want to add CarPlay to it. The app basically has a list of radio streams and the user can select which one to play.

Everything is fine if I launch the app from the phone while connected to CarPlay or if I launch it from CarPlay and device is unlocked. If the device is locked the app starts, I can see the elements on the list in CarPlay but when one of that is selected nothing happens.

I have implemented MPPlayableContentDelegate, MPPlayableContentDataSource and the app has the audio background mode selected. The streams are fetched from network but then are cached to disk.

Has everyone encountered the same issue or have any hints on how to solve it? Do you know if Apple has a simple working audio app with the CarPlay support to test out?

Habile answered 7/11, 2018 at 13:6 Comment(1)
#22372595Bracknell
H
1

I resolved the issue moving the code to setup and activate the audio session to the AppDelegate. Previously that was handled in the singleton that manages all the streamings and is the MPPlayableContentDelegate and MPPlayableContentDataSource.

Moving this to the AppDelegate solved the problem, but I still don't know why:

func startAudioSession() {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playback, mode: .default)
        try audioSession.setActive(true, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
    } catch let error as NSError {
        print("Unable to activate audio session:  \(error.localizedDescription)")
    }
}

I also added the domain of the stremaming to the App Transport Security entry in the Info.plist to allow HTTP (NSExceptionAllowsInsecureHTTPLoads). Before there was only Allow Arbitrary Loads stetted too true. Don't know if that helped.

Habile answered 13/11, 2018 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.