【问题标题】:MPNowPlayingInfoCenter doesn't update any information when after assigning to nowPlayingInfoMPNowPlayingInfoCenter 分配给 nowPlayingInfo 后不更新任何信息
【发布时间】:2017-05-31 20:19:27
【问题描述】:

我正在使用 Swift 3 制作一个 iOS 应用程序,其中在锁定屏幕和控制中心上显示有关当前播放项目的信息会很好。

目前,我正在使用以下代码尝试将此信息插入到nowPlayingInfo 字典中。我还包含了对videoBeganPlaying(_:) 中使用的VideoInfo 类的引用。

class VideoInfo {
    var channelName: String
    var title: String
}

// ...

var videoInfoNowPlaying: VideoInfo?

// ...

@objc private func videoBeganPlaying(_ notification: NSNotification?) {
    // apparently these have to be here in order for this to work... but it doesn't
    UIApplication.shared.beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()
    guard let info = self.videoInfoNowPlaying else { return }
    let artwork = MPMediaItemArtwork(boundsSize: .zero, requestHandler:
        { (_) -> UIImage in #imageLiteral(resourceName: "DefaultThumbnail") }) // this is filler
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [
            MPMediaItemPropertyTitle: info.title,
            MPMediaItemPropertyArtist: info.channelName,
            MPMediaItemPropertyArtwork: artwork
        ]
    print("Current title: ", MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyTitle])
}

该函数被调用,并执行打印语句,输出Optional("title")。但是,控制中心和锁屏不会更新它们的信息。暂停/播放,并且向前跳按钮起作用,因为我使用 MPRemoteCommandCenterviewDidLoad() 中设置它们。

怎么了?

编辑:

正如 matt 指出的那样,AVPlayerViewControllerMPNowPlayingInfoCenter 变得时髦。这是我的问题。我应该指定这是我正在使用的类,而不仅仅是AVPlayer

见: https://developer.apple.com/reference/avkit/avplayerviewcontroller/1845194-updatesnowplayinginfocenter

【问题讨论】:

    标签: ios swift swift3 mpnowplayinginfocenter


    【解决方案1】:

    它确实有效,而且您不需要关于第一响应者等等的所有花言巧语,因为您可以通过继续设置正在播放的信息来轻松向自己证明,没有别的

    那为什么它不适合你呢?可能是因为您正在使用某种播放器(例如 AVPlayerViewController)以某种方式设置正在播放的信息本身,从而覆盖您的设置。

    【讨论】:

    • 你忽略了艺术品的事情......我不太明白问题中的代码,我希望从答案中学到一些东西:)
    • 嗨@MottiShneor,很高兴收到你的来信!如果您遇到特定问题,可以单独提出问题吗?
    • 对我来说,这仅适用于模拟器,不适用于实际设备。在设备上,必须播放一些音乐才能更新控制中心:(
    • @PavelAlexeev 如果您的应用没有播放音乐,那么您的应用不是远程目标,因此远程界面不会被您的应用更新是有道理的。如果您从音乐应用播放音乐并切换到您的应用,目标仍然是音乐应用。
    • 那么,如果我们使用AVPlayerViewController,我们如何更新标题呢?但是,默认情况下(启用后台模式后),控件会显示在锁定屏幕中并响应暂停/播放。我唯一的问题是我不确定如何覆盖 AVPlayerViewController 设置的默认 nowPlayingInfo。
    【解决方案2】:

    如果您使用AVPlayerViewController,您可以指定AVPlayerViewController 实例不更新NowPlayingInfoCenter

    playerViewController.updatesNowPlayingInfoCenter = false
    

    【讨论】:

    • 一旦我将它设置为false,锁屏控件甚至不会出现。我以编程方式呈现AVPlayerViewController,但不确定这是否有任何区别。
    • 嘿@DávidPásztor 你有没有想过那个?
    • @DávidPásztor 所以我们认为,如果是 AVPlayerViewController,您根本无法按照自己的意愿调整正在播放的信息中心?
    • @matt 是的,这就是我得出的结论
    • @matt, @DávidPásztor 看来我已经设法使它与AVPlayerViewController 一起工作。当您设置updatesNowPlayingInfoCenter = false 时,信息中心不会出现在模拟器上,但似乎正在实际设备上工作。你是在物理设备上试过还是只在模拟器上试过?
    【解决方案3】:

    这让我大吃一惊。找到了一个有用的帖子。 https://nowplayingapps.com/how-to-give-more-control-to-your-users-using-mpnowplayinginfocenter/

    “为了在通知屏幕上显示 nowplayinginfo,您需要在 AppDelegate 的 didFinishLaunchingWithOptions 函数中添加最后一段代码。”

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         application.beginReceivingRemoteControlEvents()
    }
    

    【讨论】:

      【解决方案4】:

      2021 年,我在使用 AVPlayerViewController 时仍然遇到此问题。我正在开发一个同时播放播客和视频的应用程序。现在播放信息中心可以与播客一起正常工作,但即使我正确设置了MPNowPlayingInfoCenter.nowPlayingInfo,它也不会显示使用AVPlayerViewController 播放的视频的任何信息。

      虽然我没有使用 tvOS,但在观看 Now Playing and Remote Commands on tvOS 时找到了解决方案。

      基本上,我没有使用MPNowPlayingInfoCenter 填充信息,而是在AVPlayerItem 上设置了externalMetadata,它可以工作。

      let playerItem = AVPlayerItem(url: data.url)
      let title = AVMutableMetadataItem()
      title.identifier = .commonIdentifierTitle
      title.value = "Title" as NSString
      title.extendedLanguageTag = "und"
      
      let artist = AVMutableMetadataItem()
      artist.identifier = .commonIdentifierArtist
      artist.value = "Artist" as NSString
      artist.extendedLanguageTag = "und"
      
      let artwork = AVMutableMetadataItem()
      artwork.identifier = .commonIdentifierArtwork
      artwork.value = imageData as NSData
      artwork.dataType = kCMMetadataBaseDataType_JPEG as String
      artwork.extendedLanguageTag = "und"
      
      playerItem.externalMetadata = [title, artist, artwork]
      

      此代码更新现在可以正确播放信息。

      【讨论】:

        猜你喜欢
        • 2012-03-20
        • 2016-01-07
        • 2015-04-23
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        相关资源
        最近更新 更多