SwiftUI set up MPRemoteCommandCenter

516 Views Asked by At

Just trying to better understand SwiftUI...

Where/how should I set up MPRemoteCommandCenter? if I call it from OnAppear would I be creating multiple instances of MPRemoteCommandCenter.shared()?

func setupRemoteControlCenter() {
    guard let player = player else { return }
    // Get the shared MPRemoteCommandCenter
    let commandCenter = MPRemoteCommandCenter.shared()

    //Play Command
    commandCenter.playCommand.addTarget { event in
        if !player.isPlaying {
            player.play()
            self.isPlaying = true
            return .success
        }
        return .commandFailed
    }

    //Pause Command
    commandCenter.pauseCommand.addTarget { event in
        if player.isPlaying {
            player.pause()
            self.isPlaying = false
            return .success
        }
        return .commandFailed
    }
}
0

There are 0 best solutions below