Programmatically get name and artist of currently playing track in Swift

I've tried the following:

let nowPlaying = MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo

      

However, I get back nil

every time I run it when the song is playing.

I would like to be able to grab the track title and artist and display it in my application.

+3


source to share


3 answers


You are going about it completely wrong. MPNowPlayingInfoCenter has nothing to do with learning what is currently playing. If you want to know what the music app is playing right now, ask "iPod music player" (it's called in iOS 8 MPMusicPlayerController.systemMusicPlayer

).



0


source


This is not an API to get current information about game items from Music or another application, but to tell the system that your application is currently playing something and provide it with the information it needs to display on the lock screen.



So, basically what you are trying to do will not work as you expect.

0


source


Have you installed them?

var audioPlayer:MPMoviePlayerController=MPMoviePlayerController()

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [
    MPMediaItemPropertyAlbumTitle: "Album Title",
    MPMediaItemPropertyTitle: "Title",
    MPNowPlayingInfoPropertyElapsedPlaybackTime: audioPlayer.currentPlaybackTime,
    MPMediaItemPropertyPlaybackDuration: audioPlayer.duration]

      

The Playback Information Center now supports the following media element property keys:

  • MPMediaItemPropertyAlbumTitle

  • MPMediaItemPropertyAlbumTrackCount

  • MPMediaItemPropertyAlbumTrackNumber

  • MPMediaItemPropertyArtist

  • MPMediaItemPropertyArtwork

  • MPMediaItemPropertyComposer

  • MPMediaItemPropertyDiscCount

  • MPMediaItemPropertyDiscNumber

  • MPMediaItemPropertyGenre

  • MPMediaItemPropertyPersistentID

  • MPMediaItemPropertyPlaybackDuration

  • MPMediaItemPropertyTitle

-2


source







All Articles