information is not displayed in now playing

92 views Asked by At

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.

0

There are 0 answers