I have created a music application and now I wanted to add a feature to control the music from the lock screen and control center but I am getting the error, MPRemoteCommandCenter not in scope.
Here is how my code looks.
I am developing in XCode 12, but for iOS 12.4.
import UIKit
import AVKit
class SongViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
setupRemoteTransportControls()
setupNowPlaying()
}
func setupRemoteTransportControls() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { [unowned self] event in
print("Play command - is playing: \(self.player.isPlaying)")
if !self.player.isPlaying {
self.play()
return .success
}
return .commandFailed
}
// Add handler for Pause Command
commandCenter.pauseCommand.addTarget { [unowned self] event in
print("Pause command - is playing: \(self.player.isPlaying)")
if self.player.isPlaying {
self.pause()
return .success
}
return .commandFailed
}
}
You need to import the MediaPlayer
framework
import UIKit
import AVKit
import MediaPlayer
...