0

I'm playing a mp4 on a remote url everything works even it plays in background the problem is that it doesn't show the information in the now playing bar it only shows the name of my project.

mention that when I put this line if the info is shown but the controls stop working

playerViewController.updatesNowPlayingInfoCenter = false
class PlayerViewController: UIViewController {
    
    var player: AVPlayer!
    var playerViewController: AVPlayerViewController!
    var videoURLString: String = ""
    @IBOutlet weak var tittleLabel: UILabel!
    var videoTittle: String = ""
    @IBOutlet weak var playerViewUI: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 13.0, *) {
            overrideUserInterfaceStyle = .light
        }
        
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback)
            try AVAudioSession.sharedInstance().setActive(true)
        } catch {
            print("Error configuring audio session: \(error)")
        }

        tittleLabel.text = videoTittle
        guard let videoURL = URL(string: videoURLString) else {
            return
        }
        
        player = AVPlayer(url: videoURL)
        playerViewController = AVPlayerViewController()
        playerViewController.player = player
        addChild(playerViewController)
        playerViewUI.addSubview(playerViewController.view)
        playerViewController.view.frame = playerViewUI.bounds
        player.play()
        NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        setupNowPlaying()
    }
    
    @objc func playerDidFinishPlaying(_ notification: Notification) {
        navigationController?.popViewController(animated: true)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        player?.pause()
    }
    
    func setupNowPlaying() {
            var nowPlayingInfo = [String : Any]()
            nowPlayingInfo[MPMediaItemPropertyTitle] = "title example"
            nowPlayingInfo[MPMediaItemPropertyArtist] = "artist example"

            MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
        }
}

please help I don't know why the info is not being displayed.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Noise182
  • 36
  • 3
  • There is bunch of other keys have you tried that? – SPatel Jul 05 '23 at 17:56
  • i tried with other keys but they didn't appear either i made a print to "new plating info" and the print shows everything correct the problem is that it is not seen in the control center only the player is seen but not the information. – Noise182 Jul 05 '23 at 23:24
  • https://stackoverflow.com/questions/41685796/mpnowplayinginfocenter-doesnt-update-any-information-when-after-assigning-to-no – SPatel Jul 06 '23 at 07:14

0 Answers0