Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

ios12

carplay

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?

like image 914
LorenzOliveto Avatar asked Nov 07 '18 13:11

LorenzOliveto


1 Answers

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.

like image 165
LorenzOliveto Avatar answered Nov 13 '22 13:11

LorenzOliveto