0

How do I set the title and image of now playing audio in background using the AVPlayer?

My code so far:

  let path: String = String(format: "pathtoaudio")
    let aPlayerItem: AVPlayerItem = AVPlayerItem(URL: NSURL(string: path)!)
    let anAudioStreamer: AVPlayer = AVPlayer(playerItem: aPlayerItem)
    player = anAudioStreamer
    let playerController = AVPlayerViewController()
    playerController.player = player
    self.addChildViewController(playerController)
    self.view.addSubview(playerController.view)
    playerController.view.frame = self.view.frame
    player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
    let nowPlayingInfo = [MPMediaItemPropertyArtist : "Artist!",  MPMediaItemPropertyTitle : "Title!", MPMediaItemPropertyArtwork : MPMediaItemArtwork(image: UIImage(named: "nour.jpg")!)]
    MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

I am trying to use MPNowPlayingInfoCenter but am not seeing the information on the lock screen.

Mhmd Backer Shehadi
  • 559
  • 1
  • 12
  • 30

1 Answers1

4

Your Title and Artist keys look correct, but to set the album artwork you need to use the MPMediaItemArtwork object:

let nowPlayingInfo = [MPMediaItemPropertyArtist : "Artist!",  MPMediaItemPropertyTitle : "Title!", MPMediaItemPropertyArtwork : MPMediaItemArtwork(image: UIImage(...))]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo

You may also need to explicitly subscribe to remote control events as well:

UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
JAL
  • 41,701
  • 23
  • 172
  • 300
  • I did that , also I am not seeing the information in the lock screen , please check my code edit above – Mhmd Backer Shehadi Dec 07 '15 at 08:03
  • Well, tried this, and it simply doesn't work for me. No trace on the iOS (or AppleTV I'm casting to) UI. However- when I start casting, there is a transient message on the AppleTV screen, with the URL of the item I'm playing. Apple documentation on the subject is horrible, to non-existent, and this answer only added new agony... – Motti Shneor Jul 13 '17 at 10:02