From d789b7fbed9c726a370cdd4a7b08768011101c60 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 25 Sep 2022 12:16:39 -0400 Subject: [PATCH] Play sound via system player --- LiveApple/Info.plist | 4 ++-- LiveApple/PlayerController.swift | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/LiveApple/Info.plist b/LiveApple/Info.plist index d1d8014..f050495 100644 --- a/LiveApple/Info.plist +++ b/LiveApple/Info.plist @@ -2,12 +2,12 @@ + NSAppleMusicUsageDescription + bad apple BGTaskSchedulerPermittedIdentifiers task - CFBundleDocumentTypes - NSSupportsLiveActivities UIBackgroundModes diff --git a/LiveApple/PlayerController.swift b/LiveApple/PlayerController.swift index 68bdc66..bc4a821 100644 --- a/LiveApple/PlayerController.swift +++ b/LiveApple/PlayerController.swift @@ -63,6 +63,18 @@ class PlayerController: ObservableObject { // using the AVPlayer gives you sound // self.player.play() + // play song via system music player so it keeps playing in the background and sessionkitd things we're not playing media + let player = MPMusicPlayerController.systemMusicPlayer + let query = MPMediaQuery.songs() + query.addFilterPredicate(MPMediaPropertyPredicate(value: "badapple", forProperty: MPMediaItemPropertyTitle)) + player.setQueue(with: query) + do { + try await player.prepareToPlay() + player.play() + } catch { + // if the song doesn't exist, ignore it + } + // doing it manually lets you update while in the background let start = Date() timer = .scheduledTimer(withTimeInterval: 1/30, repeats: true, block: { [unowned self] _ in