I am implementing Siri/Shortcuts for my audio app for iOS. I have implemented AppIntent that sends notification to app and app should start playing the stream in AVPlayer.
AppIntent sometimes works, sometimes it doesn’t. So far I couldn’t find the pattern when/why it works and when/why it doesn’t. Sometimes it works even if app is killed or is in the background. Sometimes it doesn’t work when the app is in the background.
I have been observing logs in Console and apparently sometimes it stops when AVPlayer tries to figure out buffer size getting this log in console AVPlayerWaitingToMinimizeStallsReason and the AVPlayerItem status is set to .unknown. Sometimes it figures out quickly (for the same stream) and starts playing.
Sometimes when the app is killed app is launched in the background (at least I see it as process name in Console) and receives notification from AppIntent and start playing. Sometimes… the app is not called at all, and it is not visible in the console, so it doesn’t receives the notification and doesn’t play.
I have setup Session correctly (set to .playback without any options and activated), I set AVPlayerItem preferredForwardBufferDuration
to 0 (default), and AVPlayer automaticallyWaitsToMinimizeStalling
to true
.
// AppIntent
@available(iOS 16, *)
struct PlayCurrentStationIntent: AudioPlaybackIntent {
static let title: LocalizedStringResource = "Start playing"
static let description = IntentDescription("Plays currently selected radio")
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let player = Player.shared
NotificationCenter.default.post(name: IntentNotifications.siriPlayStream, object: nil)
return .result(dialog: "Playing (player.station.title)")
}
}
// Main
@main
struct MyApp: App {
let player = Player.shared
let siriPlayCurrentPub = NotificationCenter.default.publisher(for: IntentNotifications.siriPlayNotificationName)
...
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(player)
.onReceive(siriPlayCurrentPub, perform: { _ in
player.play()
})
}
}