Play sound via system player

This commit is contained in:
Shadowfacts 2022-09-25 12:16:39 -04:00
parent b545a3bc05
commit d789b7fbed
2 changed files with 14 additions and 2 deletions

View File

@ -2,12 +2,12 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppleMusicUsageDescription</key>
<string>bad apple</string>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>task</string>
</array>
<key>CFBundleDocumentTypes</key>
<array/>
<key>NSSupportsLiveActivities</key>
<true/>
<key>UIBackgroundModes</key>

View File

@ -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