iOS 7 rewind songs on lock screen using AVPlayer

前端 未结 4 1657
情话喂你
情话喂你 2020-12-17 00:55

I havn\'t found the way how to rewind song from lock screen iOS 7. Volume slider is available, all the buttons work correctly but the upper slider is not available!

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 01:39

    You have to register for different events like below in swift

        MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
        MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
        MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
        MPRemoteCommandCenter.shared().stopCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
        MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
        MPRemoteCommandCenter.shared().changePlaybackPositionCommand.isEnabled = true
        MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(self.handleChangePlaybackPositionRemoteCommandActions))
    
        let rcc = MPRemoteCommandCenter.shared()
        let skipBackwardIntervalCommand: MPSkipIntervalCommand? = rcc.skipBackwardCommand
        skipBackwardIntervalCommand?.isEnabled = false
        let skipForwardIntervalCommand: MPSkipIntervalCommand? = rcc.skipForwardCommand
        skipForwardIntervalCommand?.isEnabled = false
        let seekForwardCommand: MPRemoteCommand? = rcc.seekForwardCommand
        seekForwardCommand?.isEnabled = true
        rcc.changePlaybackPositionCommand.isEnabled = true
        rcc.changePlaybackRateCommand.isEnabled = true
        rcc.ratingCommand.isEnabled = true
        rcc.playCommand.isEnabled = true
        rcc.togglePlayPauseCommand.isEnabled = true
    

    here handleChangePlaybackPositionRemoteCommandActions this method will be your method which will have to manage seeking of song when scrubber (upper slider) changes its value

    it will look something like below:-

    @objc func handleChangePlaybackPositionRemoteCommandActions(event:MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus
    {
        print("handleChangePlaybackPositionRemoteCommandActions : \(event.positionTime)")
        self.appDelegate.audioPlayer?.seek(to: CMTime(seconds: event.positionTime, preferredTimescale: (self.appDelegate.audioPlayer?.currentItem?.currentTime().timescale)!))
    
        MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = event.positionTime
    
        return MPRemoteCommandHandlerStatus.success
    }
    

提交回复
热议问题