AVPlayer Lock Screen Controls

后端 未结 1 563
独厮守ぢ
独厮守ぢ 2021-01-01 00:07

I have an app that plays audio on the background using AVPlayer. I use MPNowPlayingInfoCenter to display the song\'s metadata on the Lock Screen and Control Center. Everythi

1条回答
  •  离开以前
    2021-01-01 00:57

    Rather than using the UIEvent stream with remoteControlReceivedWithEvent, I would recommend you use MPRemoteCommandCenter to control the previous/next/play/pause actions on the lock screen and control center.

    import MediaPlayer
    
    // ...
    
    let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()
    
    commandCenter.previousTrackCommand.enabled = true;
    commandCenter.previousTrackCommand.addTarget(self, action: "previousTrack")
    
    commandCenter.nextTrackCommand.enabled = true
    commandCenter.nextTrackCommand.addTarget(self, action: "nextTrack")
    
    commandCenter.playCommand.enabled = true
    commandCenter.playCommand.addTarget(self, action: "playAudio")
    
    commandCenter.pauseCommand.enabled = true
    commandCenter.pauseCommand.addTarget(self, action: "pauseAudio")
    

    Where previousTrack, nextTrack, playAudio, and pauseAudio are all functions in your class where you control the player.

    You may need to explicitly disable the skip forward and backward commands as well:

    commandCenter.skipBackwardCommand.enabled = false
    commandCenter.skipForwardCommand.enabled = false
    

    0 讨论(0)
提交回复
热议问题