Manage music on the iPhone lock screen

I am developing a music application, but to control the screen lock, I can not assign the length of time and elapsed time.

here is my code

let commandCenter = MPRemoteCommandCenter.shared() commandCenter.previousTrackCommand.isEnabled = true; commandCenter.previousTrackCommand.addTarget(self, action:#selector(home_ViewController.btn_rewind(_:))) commandCenter.nextTrackCommand.isEnabled = true commandCenter.nextTrackCommand.addTarget(self, action:#selector(home_ViewController.btn_fast(_:))) commandCenter.playCommand.isEnabled = true commandCenter.playCommand.addTarget(self, action:#selector(home_ViewController.play_player)) commandCenter.pauseCommand.isEnabled = true commandCenter.pauseCommand.addTarget(self, action:#selector(home_ViewController.pause_player)) commandCenter.togglePlayPauseCommand.isEnabled = true commandCenter.togglePlayPauseCommand.addTarget(self, action:#selector(home_ViewController.togglePlay_Pause)) commandCenter.skipBackwardCommand.isEnabled = false commandCenter.skipForwardCommand.isEnabled = false if #available(iOS 9.1, *) { commandCenter.changePlaybackPositionCommand.isEnabled = true } else { // Fallback on earlier versions return } 

and media information

 func setLockInfo() { let url = URL(string: song_UrlString) let data = try? Data(contentsOf: url!) let art = MPMediaItemArtwork.init(image: UIImage(data: data!)!) let songInfo :[String : Any] = [MPMediaItemPropertyTitle :st_title,MPMediaItemPropertyArtwork : art] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo } 

I get the title and image, but the lock screen does not display the time

I am using SWIFT 3 for code

+5
source share
2 answers

He does not display time, because you do not show him time.

To show playback time, your nowPlayingInfo dictionary should include values ​​for keys:

If you want the playtime bar to be interactive (i.e. allow switching to another time instead of just displaying the current time, register changePlaybackPositionCommand at the remote command center.

+4
source

Swift 3 example with all woking commands, including changePlaybackPositionCommand :

 func remotePlayerInit() { UIApplication.shared.beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.shared() commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:))) commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:))) commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:))) commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:))) commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:))) setLockInfo() } func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus { audioPlayer.currentTime = event.positionTime setLockInfo() return .success } func setLockInfo() { let image = PlayerVC.songs[PlayerVC.currentSelection].image let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in return image }) let songInfo : [String : Any] = [MPMediaItemPropertyTitle : PlayerVC.songs[PlayerVC.currentSelection].name, MPMediaItemPropertyArtwork : artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime : TimeInterval(audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate : 1, MPMediaItemPropertyPlaybackDuration : audioPlayer.duration, MPMediaItemPropertyArtist : "Artist"] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo } = [MPMediaItemPropertyTitle: PlayerVC.songs [PlayerVC.currentSelection] .name, MPMediaItemPropertyArtwork: artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime: TimeInterval (audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate: func remotePlayerInit() { UIApplication.shared.beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.shared() commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:))) commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:))) commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:))) commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:))) commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:))) setLockInfo() } func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus { audioPlayer.currentTime = event.positionTime setLockInfo() return .success } func setLockInfo() { let image = PlayerVC.songs[PlayerVC.currentSelection].image let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in return image }) let songInfo : [String : Any] = [MPMediaItemPropertyTitle : PlayerVC.songs[PlayerVC.currentSelection].name, MPMediaItemPropertyArtwork : artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime : TimeInterval(audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate : 1, MPMediaItemPropertyPlaybackDuration : audioPlayer.duration, MPMediaItemPropertyArtist : "Artist"] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo } Artist func remotePlayerInit() { UIApplication.shared.beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.shared() commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:))) commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:))) commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:))) commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:))) commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:))) setLockInfo() } func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus { audioPlayer.currentTime = event.positionTime setLockInfo() return .success } func setLockInfo() { let image = PlayerVC.songs[PlayerVC.currentSelection].image let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in return image }) let songInfo : [String : Any] = [MPMediaItemPropertyTitle : PlayerVC.songs[PlayerVC.currentSelection].name, MPMediaItemPropertyArtwork : artwork, MPNowPlayingInfoPropertyElapsedPlaybackTime : TimeInterval(audioPlayer.currentTime), MPNowPlayingInfoPropertyPlaybackRate : 1, MPMediaItemPropertyPlaybackDuration : audioPlayer.duration, MPMediaItemPropertyArtist : "Artist"] MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo } 
0
source

Source: https://habr.com/ru/post/1259234/


All Articles