brotato
brotato

Reputation: 41

Siri Shortcut INPlayMediaIntent stops playing after 2 minutes

I am integrating our music service app with new Shortcuts feature and stuck with weird SiriKit extension behaviour. My INPlayMediaIntentHandling implementation downloads music tracks and starts playing them with AVPlayer, but extension exits with code 0 after 120 seconds on each launch(Xcode console says "Program ended with exit code: 0"). I tried to search but Apple documentation is empty on this topic and 2018 WWDC session has only brief information for this Intent.

There's a sample on GitHub — https://github.com/beryu/SiriShortcutsSample but its using Apple Music 30 seconds samples. I tried to use it with long mp3 stream, but it behaves like my app and stops after 2 minutes.

Has anyone successfully implemented INPPlayMediaIntent?

IntentHandler code

class IntentHandler: INExtension, INPlayMediaIntentHandling {

private var intentCompletion: Any?

@available(iOSApplicationExtension 12.0, *)
func handle(intent: INPlayMediaIntent, completion: @escaping (INPlayMediaIntentResponse) -> Void) {

    intentCompletion = completion
    switch intent.mediaContainer?.identifier {
    case ShortcutsMeta.FavouritesIdentifier: startFavourites()

    default: complete(.success);
    }
}

@available(iOSApplicationExtension 12.0, *)
private func complete(_ resultCode: INPlayMediaIntentResponseCode) {
    let userActivity = NSUserActivity(activityType: NSStringFromClass(INPlayMediaIntent.self))
    let response = INPlayMediaIntentResponse(code: resultCode, userActivity: userActivity)
    (intentCompletion as? ((INPlayMediaIntentResponse) -> Void))?(response)
    intentCompletion = nil
}

override func handler(for intent: INIntent) -> Any {
    return self
}

private func startFavourites() {
    let command = IntentCommand(type: .favourites)
    // this code loads track and calls AVPlayer to play it, then calls completion
    command.run{ [weak self] (result: Bool) in
        if #available(iOSApplicationExtension 12.0, *) {
            self?.complete(result ? .success : .failure)
        }
    }
}

}

Upvotes: 2

Views: 825

Answers (1)

brotato
brotato

Reputation: 41

I had a conversation with an Apple engineer and he said that INPlayMediaIntentHandling can only be used with .handleInApp intent response result code. So, I moved intent handler logic in app main target and call it via AppDelegate's -application:handleIntent:completionHandler:

Anything works good, except AudioSession interruptions from Siri — she stops my media playing logic immediately after start. That engineer from Apple said that this is an iOS bug and Apple is working on bug fix(maybe in 12.1 they fixed it, I don't checked yet)

Upvotes: 2

Related Questions