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"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>NSAppleMusicUsageDescription</key>
<string>bad apple</string>
<key>BGTaskSchedulerPermittedIdentifiers</key> <key>BGTaskSchedulerPermittedIdentifiers</key>
<array> <array>
<string>task</string> <string>task</string>
</array> </array>
<key>CFBundleDocumentTypes</key>
<array/>
<key>NSSupportsLiveActivities</key> <key>NSSupportsLiveActivities</key>
<true/> <true/>
<key>UIBackgroundModes</key> <key>UIBackgroundModes</key>

View File

@ -63,6 +63,18 @@ class PlayerController: ObservableObject {
// using the AVPlayer gives you sound // using the AVPlayer gives you sound
// self.player.play() // 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 // doing it manually lets you update while in the background
let start = Date() let start = Date()
timer = .scheduledTimer(withTimeInterval: 1/30, repeats: true, block: { [unowned self] _ in timer = .scheduledTimer(withTimeInterval: 1/30, repeats: true, block: { [unowned self] _ in