idearibosome
idearibosome

Reputation: 684

How to set current playback duration and elapsed time on iOS 7 lockscreen?

Starting from iOS 5, every music player can set current playing music information such as title, artist, album title, and artwork on [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo to show on lock screen.

On iOS 7, playback position slider, duration, and elapsed time information are added to both lock screen and control center. However, I cannot find any documents to set these kinds of information and enable the slider to change playback position.

Is there any way to solve this problem?

Upvotes: 19

Views: 16116

Answers (3)

ren6
ren6

Reputation: 564

You need to setup playback rate to 1.0f even if documentation says it's 1.0 by default.

NSDictionary *mediaInfo = @{
    MPMediaItemPropertyTitle: audio.title,
    MPMediaItemPropertyArtist: audio.artist,
    MPMediaItemPropertyPlaybackDuration: audio.duration,
    MPNowPlayingInfoPropertyPlaybackRate: @(1.0)
};

[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];

Upvotes: 37

xyz
xyz

Reputation: 135

Just be warned: Apple's document never made this clear -- If you use MPMusicPlayerController, your music is played under the hood by the "music" app and you do NOT have any control of nowPlayingInfoCenter. And you will NOT receive remote control events generated by the user actions (such as play/pause) applied to the lock screen because those events are propagated via the nowPlayingInfoCenter to the "music" app, not to yours. When using other media players, such as AV or AvAudio, you can control the nowPlayingInfoCenter and receive the remote control events. But if you use AVAudioSessionCategoryOptions.MixWithOthers to set up the AV player, you can't control nowPlayingInfoCenter either. I wish Apple documented those details better.

Upvotes: 4

Anya Shenanigans
Anya Shenanigans

Reputation: 94654

They're all documented in the reference for MPNowPlayingInfoCenter. The currently playing properties are optional values that may or may not be set. The link to that is in the sentence at the end of the list of normal playing properties:

Additional properties you can set are described in this document in “Additional Metadata Properties.”. (emphasis mine)

The properties that you are interested in are: MPNowPlayingInfoPropertyElapsedPlaybackTime and MPMediaItemPropertyPlaybackDuration.

This information is all publicly available, and as the iOS 7 SDK does not seem to be published yet (as of 2013-09-14), I presume it was available prior to that version of iOS as well.

Upvotes: 8

Related Questions