MPRemoteCommandCenter doesn't appear on iOS13

3k Views Asked by At

I have a video player that show a .m3u8 stream. I've tried to implement the Remote Control but the widget doesn't appear both in lock screen, in the Notification Center and in the Command Center.

I need only to have play-pause, volume controls and some static info as Title, Artist and Artwork.

This is my code:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error.localizedDescription)
    }

    return true
}

ViewController.swift

   class ViewController: UIViewController {

    var player = AVPlayer()
    var playerViewcontroller = AVPlayerViewController()
    var playerItem: AVPlayerItem!
    var playerLayer = AVPlayerLayer()

    @IBAction func playVideo(_ sender: Any) {
        guard let url = URL(string: "https://video.m3u8") else {
            return
        }

        // Create an AVPlayer, passing it the HTTP Live Streaming URL.
        playerItem = AVPlayerItem(url: url)
        playerItem.preferredForwardBufferDuration = 8
        player = AVPlayer(playerItem: playerItem)

        // Create a new AVPlayerViewController and pass it a reference to the player.
        playerViewcontroller.player = player
        playerViewcontroller.showsPlaybackControls = true

        // Modally present the player and call the player's play() method when complete.
        present(playerViewcontroller, animated: true) {
            self.player.play()
        }

        setupNowPlaying()

    }

    override func viewDidLoad() {
        //super.viewDidLoad()
    }

    public func disconnectAVPlayer() {
        playerViewcontroller.player = nil
    }

    public func reconnectAVPlayer() {
        playerViewcontroller.player = player
    }


    func setupNowPlaying() {
        print("_________________________________setupPlaying")
        // Define Now Playing Info
        let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
        var nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo ?? [String: Any]()

        let title = "TV NAME"
        let album = "TV DESCRIPTION"
        let image = UIImage(named: "ICON") ?? UIImage()
        let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: {  (_) -> UIImage in
          return image
        })

        nowPlayingInfo[MPMediaItemPropertyTitle] = title
        nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album
        nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = NSNumber(value: 1.0)
        nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo
    }

}

Hope that infos are sufficient. Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

Remote Control Works fine in iOS 13 , you just didn’t set it up first. Add this code after assigning to your AVPlayer

func commandCenterSetup() {

    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()


   setupNowPlaying()

    commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in

        print("PAUSE")
        self.playerViewcontroller.player.pause()
        return .success

       }


       commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in

        print("PLAY")
        playerViewcontroller.player.play()
        return .success
       }



    }