Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Music control at lock screen iPhone

I am developing music app but for lock screen control i am not able to assign time duration 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 info

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 am getting title and image but lock screen is not displaying time

I am using SWIFT 3 to code

like image 413
Rakshit Korat Avatar asked Nov 03 '16 15:11

Rakshit Korat


2 Answers

It's not displaying the time because you're not telling it to display the time.

To show the playback time, your nowPlayingInfo dictionary needs to include values for the keys:

  • MPNowPlayingInfoPropertyElapsedPlaybackTime, so it knows what the current time is when you start playing,
  • MPMediaItemPropertyPlaybackDuration, so it knows what the current time is relative to in the bar, and
  • MPNowPlayingInfoPropertyPlaybackRate, so it can automatically update the playback time UI without you needing to periodically set the current time.

If you want the playback time bar to be interactive (that is, allow jumping to a different time instead of just displaying the current time, register a changePlaybackPositionCommand with your remote command center.

like image 185
rickster Avatar answered Sep 20 '22 20:09

rickster


Swift 3 example with all commands woking, 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
}
like image 43
coldembrace Avatar answered Sep 17 '22 20:09

coldembrace