6

In objective c, I've been using code like this to update MPNowPlayingInfoCenter:

[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo: 
    @{ MPMediaItemPropertyArtist : @"Artist!",
        MPMediaItemPropertyTitle : @"Title! }];

But in Swift, it doesn't seem like the function "setNowPlayingInfo" is recognized:

MPNowPlayingInfoCenter.defaultCenter()....  // Can't identify 'setNowPlayingInfo()'

Is there anything I'm missing?

JOM
  • 8,139
  • 6
  • 78
  • 111
jmkr
  • 155
  • 2
  • 9
  • 1
    try `MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyArtist : "Artist!", MPMediaItemPropertyTitle : "Title!]`, setters/getters behave quite differently in Swift from ObjC – Jack Jun 17 '14 at 18:43
  • @JackWu thanks so much. Yep, still getting the hang of Swift. – jmkr Jun 17 '14 at 18:56

3 Answers3

5

In Swift, getters/setters work differently. Since there are no more properties like in ObjC, there are no automatically generated setters/getters for you. You should just access the variable directly.

In your case, use:

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyArtist : "Artist!",  MPMediaItemPropertyTitle : "Title!"]
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
Jack
  • 16,677
  • 8
  • 47
  • 51
  • As of today, i can't get it to work... get a error **type "String" does not conform to protocol 'AnyObject'** – KennyVB Jan 14 '15 at 19:12
1

Swift 2, this also work:

let songInfo: [String:AnyObject] = [
         MPMediaItemPropertyTitle: mySoundTrack.TrackName,
         MPMediaItemPropertyArtist: String(mySoundTrack.TrackID),
         MPMediaItemPropertyArtwork: albumArt
]

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo
Taimur Ajmal
  • 2,778
  • 6
  • 39
  • 57
Franck
  • 8,939
  • 8
  • 39
  • 57
0

Previous answer by @Jack no longer works as is, and needs some modifications.

Swift 3 compiler complains about 2 things.

  • String is not directly convertible to AnyObject, and so the two dictionary entry values must be casted from String to AnyObject.
  • defaultCenter() has been renamed to default().

so a viable code will look like this:

MPNowPlayingInfoCenter.default().nowPlayingInfo = [MPMediaItemPropertyArtist : "Artist!" as AnyObject,  MPMediaItemPropertyTitle : "Title!" as AnyObject]
Motti Shneor
  • 2,095
  • 1
  • 18
  • 24