command center Scrubber on lock screen swift

最后都变了- 提交于 2021-01-28 08:01:09

问题


I am trying to add scrubber to command center lock screen, I am getting this error Cannot assign to value: function call returns immutable value I don't know what it means. Any help would be appreciated.

This is how I am trying to change the position

commandCenter.changePlaybackPositionCommand.addTarget(handler: {
        (event) in
        let event = event as! MPChangePlaybackPositionCommandEvent
        self.player.currentTime() = event.positionTime  // ERROR
        return MPRemoteCommandHandlerStatus.success
    })

回答1:


I think your player property is an AVPlayer (???), and if so you want to use the seek function to set the currentTime, not set the return value from the function...

self.player.seek(to: CMTimeMakeWithSeconds(event.positionTime, 1000000))



回答2:


Firstly you have to set up the nowPlaying metadata and you call this whenever you change anything.

//MARK: setupNowPlaying----------------------------------
func setupNowPlaying() {
    // Define Now Playing Info
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = self.nowPlayingString
    let image = UIImage(named: "Somni-lockLogo")! // this is the image you want to see on the lock screen
    let artwork = MPMediaItemArtwork.init(boundsSize: image.size,
                                          requestHandler: { (size) -> UIImage in
        return image
    })
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self.player.currentTime
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = self.player.duration
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
    //MARK: now playing
    nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
    nowPlayingInfo[MPMediaItemPropertyArtist ] = self.nowPlayingTitle
    // other metadata exists, check the documentation
  //  nowPlayingInfo[MPMediaItemPropertyArtist] = "David Bowie"
  //  nowPlayingInfo[MPMediaItemPropertyComposer] = "Bill Gates"
    
    // Set the metadata
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

then you need to set up the remote transport controls enabling the things you need like play pause skip etc Here is the start of my function which enables stuff and includes to code to make the scrubber work

    func setupRemoteTransportControls() {
    // Get the shared MPRemoteCommandCenter
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.isEnabled = true
    commandCenter.pauseCommand.isEnabled = true
     let skipBackwardIntervalCommand: MPSkipIntervalCommand? = commandCenter.skipBackwardCommand
    let skipForwardIntervalCommand: MPSkipIntervalCommand? = commandCenter.skipForwardCommand
    let seekForwardCommand: MPRemoteCommand? = commandCenter.seekForwardCommand
    let seekBackwardCommand: MPRemoteCommand? = commandCenter.seekBackwardCommand
    seekForwardCommand?.isEnabled = true
    seekBackwardCommand?.isEnabled = true
   
    skipBackwardIntervalCommand!.isEnabled = true
    skipForwardIntervalCommand!.preferredIntervals = [60]
    skipBackwardIntervalCommand!.preferredIntervals = [60]
    commandCenter.changePlaybackPositionCommand.isEnabled = true
    
    commandCenter.changePlaybackPositionCommand.addTarget
        { (event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus in
            let event = event as! MPChangePlaybackPositionCommandEvent
            print("change playback",event.positionTime)
            self.player.currentTime = event.positionTime
            self.setupNowPlaying()
            return .success
        }

// etc etc whatever you want to use needs a handler and you need to set the event type to the correct one in the handler.



来源:https://stackoverflow.com/questions/51288634/command-center-scrubber-on-lock-screen-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!