简体   繁体   English

播放歌曲时播放/暂停按钮不更新

[英]The play/pause button does not update when play song

I'm building an music app that can integrate with CarPlay, everything is all right... I can display artist name, title, album name and some button controller on music player.我正在构建一个可以与 CarPlay 集成的音乐应用程序,一切都很好......我可以在音乐播放器上显示艺术家姓名、标题、专辑名称和一些按钮 controller。 However, there is an issue that when i play any song from CarPlay, the button on CarPlay is not update same as button on iOS device and seek time does not move.但是,有一个问题是,当我从 CarPlay 播放任何歌曲时,CarPlay 上的按钮不会像 iOS 设备上的按钮一样更新,并且搜索时间不会移动。

What should I do?我应该怎么办? I searched many resources to find solution but still yet.我搜索了许多资源以找到解决方案,但仍然没有。

在此处输入图像描述

在此处输入图像描述

Note: I just only test it on simulator (not real device).注意:我只是在模拟器(不是真实设备)上测试它。

This is show PlayingInfor:这是 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()
        }
    }
}

This is handle RemoteCommandEvent:这是句柄 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

Do you ever register for the remote events in your code?您是否曾经在代码中注册过远程事件? eg例如

UIApplication.shared.beginReceivingRemoteControlEvents()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM