From a6266ee3db11ed263e825324249d16bdcb016eaf Mon Sep 17 00:00:00 2001 From: Bill Date: Thu, 23 Mar 2023 02:26:26 +0800 Subject: [PATCH] fix background playback --- MusicPlayer Watch App/ContentView.swift | 4 ++-- MusicPlayer Watch App/PlaybackView.swift | 1 + .../xcdebugger/Breakpoints_v2.xcbkptlist | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/MusicPlayer Watch App/ContentView.swift b/MusicPlayer Watch App/ContentView.swift index 118e33a..dd2b5c1 100644 --- a/MusicPlayer Watch App/ContentView.swift +++ b/MusicPlayer Watch App/ContentView.swift @@ -188,7 +188,7 @@ struct ContentView: View { print(self.player.error!) } else { - //self.player.play() + self.player.play() } } init() { @@ -202,7 +202,7 @@ struct ContentView: View { do { let session = AVAudioSession.sharedInstance() - try session.setCategory(AVAudioSession.Category.playback) + try session.setCategory(AVAudioSession.Category.playback, mode: .default, policy: .longFormAudio, options: .duckOthers) } catch { print(error) } diff --git a/MusicPlayer Watch App/PlaybackView.swift b/MusicPlayer Watch App/PlaybackView.swift index ead55d8..ccd3b62 100644 --- a/MusicPlayer Watch App/PlaybackView.swift +++ b/MusicPlayer Watch App/PlaybackView.swift @@ -69,6 +69,7 @@ struct PlaybackView: View { parent!.player.pause() self.playing = false } else { + parent!.player.audiovisualBackgroundPlaybackPolicy = .continuesIfPossible parent!.player.play() self.playing = true } diff --git a/MusicPlayer.xcodeproj/xcuserdata/bill.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/MusicPlayer.xcodeproj/xcuserdata/bill.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index bc06a78..c920a37 100644 --- a/MusicPlayer.xcodeproj/xcuserdata/bill.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/MusicPlayer.xcodeproj/xcuserdata/bill.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -36,5 +36,21 @@ landmarkType = "9"> + + + +