AVPlayer Lock Controls

I have an application that plays audio in the background using AVPlayer. I use MPNowPlayingInfoCenter to display song metadata on the lock screen and control center. Everything works fine, except for one.

The remote control on the lock screen and control center are the parameters of the podcast application. They do not have back and forth buttons.

I have a screenshot of how the controls are:

enter image description here

As you can see, I do not have back and forth buttons.

override func viewDidLoad() { super.viewDidLoad() do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) UIApplication.sharedApplication().beginReceivingRemoteControlEvents() } catch { print(error) } } @IBAction func play(sender: AnyObject) { if isURLAvailable == false { return } if (player!.rate != 0 && player!.error == nil) { player!.pause() } else { player!.play() } updateImage() } func playSong(song: Song) { let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL? let url: NSURL? = documentsDirectoryURL?.URLByAppendingPathComponent(song.fileName) let avItem = AVPlayerItem(URL: url!) player = AVPlayer(playerItem: avItem) player?.play() let artworkProperty = MPMediaItemArtwork(image: song.artwork) MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyTitle : lblSongName.text!, MPMediaItemPropertyArtist : song.artist, MPMediaItemPropertyArtwork : artworkProperty, MPNowPlayingInfoPropertyDefaultPlaybackRate : NSNumber(int: 1), MPMediaItemPropertyPlaybackDuration : CMTimeGetSeconds((player!.currentItem?.asset.duration)!)] } override func remoteControlReceivedWithEvent(event: UIEvent?) { print(event!.type) if event!.type == UIEventType.RemoteControl { if event?.subtype == UIEventSubtype.RemoteControlPlay || event?.subtype == UIEventSubtype.RemoteControlPause { play(self) } if event?.subtype == UIEventSubtype.RemoteControlNextTrack { next(self) } if event?.subtype == UIEventSubtype.RemoteControlPreviousTrack { previous(self) } } } 
+3
source share
1 answer

Instead of using the UIEvent stream with remoteControlReceivedWithEvent I would recommend using MPRemoteCommandCenter to control the previous / next / play / pause actions on the lock screen and in the 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 the functions in your class where you control the player.

You may need to explicitly disable the forward and backward scrolling of commands:

 commandCenter.skipBackwardCommand.enabled = false commandCenter.skipForwardCommand.enabled = false 
+10
source

All Articles