1

StackOverflow I want to trying read current playing song from control center in ios7+ using swift lang In objective-c I could read it using this code

NSDictionary *nowPlaying = @{MPMediaItemPropertyArtist: currentTrack.artist,
                             MPMediaItemPropertyAlbumTitle: currentTrack.title};

But I don't know how write song_info_getter method in swift P.s. I couldn't find similar question on SO

Specifically, I'm having trouble getting the current track info. (artist/album/etc)

I've made this on desktop (mac):

func get_iTunes_data() {         
    NSDistributedNotificationCenter.defaultCenter().addObserver(self, selector: "allDistributedNotifications:", name: "com.apple.iTunes.playerInfo", object: "com.apple.iTunes.player")     
}

var now : String = ""      
func allDistributedNotifications (note : NSNotification) {                 
    let userInfo:NSDictionary = note.userInfo as NSDictionary        
    println(userInfo)
}

But I don't know how to make like this for iOS

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
gJamDev
  • 81
  • 1
  • 11

1 Answers1

3

On iOS this is actually painless. The MPMusicPlayerController class provides means to access the now playing item in the form of a MPMediaItem, which contains all of this information.

iOS 8+

let nowPlaying = MPMusicPlayerController.systemMusicPlayer().nowPlayingItem

Before iOS 8

let nowPlaying = MPMusicPlayerController.iPodMusicPlayer().nowPlayingItem

Then do extract individual pieces of information from the MPMediaItem that is return from the above. Keep in mind that all the different keys start with MPMediaItemProperty... but not all of them return strings. Some will return NSNumbers, etc. Consult the documentation for info on this.

let trackName = nowPlaying.valueForProperty(MPMediaItemPropertyTitle) as String

Real answer

Sorry, I did that for MPMusicPlayController. Here is how to access the info from the MPNowPlayingInfoCenter.

let nowPlaying = MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281