How can I make the Control Center slider editable?

后端 未结 6 972
栀梦
栀梦 2021-01-04 20:54

I\'m working on an app that will play sound files. If I open apple music app, the slider let me moving between the song where I am.

Other apps like spotify

6条回答
  •  情话喂你
    2021-01-04 21:26

    iOS 12+ and Swift 4+

    Following is an improved way of handling scrubbing in Remote Play Center:

    // Handle remote events
    func setupRemoteTransportControls() {
        let commandCenter = MPRemoteCommandCenter.shared()
    
        // Scrubber
        commandCenter.changePlaybackPositionCommand.addTarget { [weak self](remoteEvent) -> MPRemoteCommandHandlerStatus in
            guard let self = self else {return .commandFailed}
            if let player = self.player {
               let playerRate = player.rate
               if let event = remoteEvent as? MPChangePlaybackPositionCommandEvent {
                   player.seek(to: CMTime(seconds: event.positionTime, preferredTimescale: CMTimeScale(1000)), completionHandler: { [weak self](success) in
                       guard let self = self else {return}
                       if success {
                           self.player?.rate = playerRate
                       }
                   })
                   return .success
                }
            }
            return .commandFailed
        }
    
        // Register to receive events
        UIApplication.shared.beginReceivingRemoteControlEvents()
    }
    

提交回复
热议问题