Music control at lock screen iPhone
Asked Answered
J

2

6

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

Jessjessa answered 3/11, 2016 at 15:15 Comment(0)
L
4

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:

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.

Lukelukens answered 3/11, 2016 at 18:43 Comment(2)
let songInfo :[String : Any] = [MPMediaItemPropertyTitle :st_title,MPMediaItemPropertyArtwork : art,MPNowPlayingInfoPropertyElapsedPlaybackTime : NSNumber.init(value: (player.currentTime()).value),MPMediaItemPropertyPlaybackDuration : NSNumber.init(value: ((player.currentItem?.duration)?.value)!),MPNowPlayingInfoPropertyPlaybackRate :NSNumber.init(value: 1)] i have changed to above but still not comingJessjessa
Can please give an exampleJessjessa
G
1

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
}
Giro answered 7/1, 2018 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.