0

如何使用 设置正在后台播放音频的标题和图像AVPlayer

到目前为止我的代码:

  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()

我正在尝试使用MPNowPlayingInfoCenter,但在锁定屏幕上看不到信息。

4

1 回答 1

3

您的 Title 和 Artist 键看起来是正确的,但是要设置专辑插图,您需要使用该MPMediaItemArtwork对象:

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

您可能还需要显式订阅远程控制事件:

UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
于 2015-12-04T15:09:43.323 回答