【问题标题】:The play/pause button does not update when play song播放歌曲时播放/暂停按钮不更新
【发布时间】:2021-01-13 04:09:12
【问题描述】:

我正在构建一个可以与 CarPlay 集成的音乐应用程序,一切都很好……我可以在音乐播放器上显示艺术家姓名、标题、专辑名称和一些按钮控制器。但是,当我从 CarPlay 播放任何歌曲时,CarPlay 上的按钮不会像 iOS 设备上的按钮那样更新,并且搜索时间不会移动。

我该怎么办?我搜索了许多资源以找到解决方案,但仍然没有。

注意:我只是在模拟器(不是真实设备)上测试它。

这是 PlayingInfor 节目:

private func nowPlayingInfoOverwrite(time: CMTime) {
    if let nowPlayingItem: PlaylistItem = self.nowPlayingItem {
        let info: NSMutableDictionary = NSMutableDictionary()
        info[MPMediaItemPropertyArtist] = nowPlayingItem.mediaItem?.artist?.name
        info[MPMediaItemPropertyAlbumTitle] = nowPlayingItem.mediaItem?.album?.title
        info[MPMediaItemPropertyTitle] = nowPlayingItem.mediaItem?.title
        info[MPMediaItemPropertyPlaybackDuration] = nowPlayingItem.mediaItem?.playbackDuration
        info[MPMediaItemPropertyArtwork] = nowPlayingItem.mediaItem?.artwork()

        if self.playbackState != .playing {
            info[MPNowPlayingInfoPropertyPlaybackRate] = 1e-6
        } else {
            info[MPNowPlayingInfoPropertyPlaybackRate] = 1
        }
        let sec: TimeInterval = CMTimeGetSeconds(time)
        info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Int(sec)
        MPNowPlayingInfoCenter.default().nowPlayingInfo = info as? [String: Any]
    } else {
        if !MoviePlayerController.instance.isPlaying() {
            MPNowPlayingInfoCenter.default().nowPlayingInfo = Dictionary()
        }
    }
}

这是句柄 RemoteCommandEvent:

    let center: MPRemoteCommandCenter = MPRemoteCommandCenter.shared()
    center.pauseCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
    center.playCommand.addTarget(self, action: #selector(remoteCommandPlay(_ :)))
    center.stopCommand.addTarget(self, action: #selector(remoteCommandStop(_ :)))
    center.togglePlayPauseCommand.addTarget(self, action: #selector(remoteCommandTogglePlayPause(_ :)))
    center.nextTrackCommand.addTarget(self, action: #selector(remoteCommandNextTrack(_ :)))
    center.previousTrackCommand.addTarget(self, action: #selector(remoteCommandPreviousTrack(_ :)))
    center.nextTrackCommand.isEnabled = true
    center.previousTrackCommand.isEnabled = true

    center.changeShuffleModeCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
    center.likeCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
    center.changeRepeatModeCommand.addTarget(self, action: #selector(remoteCommandPause(_ :)))
    center.changeShuffleModeCommand.isEnabled = true
    center.likeCommand.isEnabled = true
    center.changeRepeatModeCommand.isEnabled = true

【问题讨论】:

    标签: ios swift mpnowplayinginfocenter carplay mpremotecommandcenter


    【解决方案1】:

    您是否曾经在代码中注册过远程事件?例如

    UIApplication.shared.beginReceivingRemoteControlEvents()
    

    【讨论】:

    • 是的,我在播放音乐时注册了远程事件控制。 UIApplication.shared.endReceivingRemoteControlEvents() UIApplication.shared.beginReceivingRemoteControlEvents() completionHandler(nil)
    • 我找到了一个解决方案,即设置 if #available(iOS 13, *) { MPNowPlayingInfoCenter.default().playbackState = isPlaying ? .playing : .paused } 显示“正在播放信息”屏幕时,但此语句在 iOS 14 而不是 iOS 13 上有效,尽管我在设置之前检查了 iOS 版本。
    • 所以在模拟器上不起作用的一件事是播放按钮(它永远不会像在真实设备上那样变成暂停按钮)。在发布之前,您绝对应该检查您在真实设备上的实现。至于经过的时间:你能检查一下你是否每秒钟都设置了MPNowPlayingInfoPropertyElapsedPlaybackTime?通知中心的进度实际上应该与iOS上的相同。在实施过程中,我们还发现了一些奇怪的行为,我们在此处记录了这些行为:medium.com/br-next/…也许有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2018-07-17
    • 2023-04-03
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多