簡體   English   中英

播放歌曲時播放/暫停按鈕不更新

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

我正在構建一個可以與 CarPlay 集成的音樂應用程序,一切都很好......我可以在音樂播放器上顯示藝術家姓名、標題、專輯名稱和一些按鈕 controller。 但是,有一個問題是,當我從 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

您是否曾經在代碼中注冊過遠程事件? 例如

UIApplication.shared.beginReceivingRemoteControlEvents()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM