Search code examples
iphoneiosnotificationsmpmediaitem

Is there a way to get information about the previous iPod song?


I'm trying to store the playhead time of a song (MPMediaItem) before the next song begins to play. How would you do that?

Is there something like MPMusicPlayerControllerNowPlayingItemDidChangeNotification which fires before the song changes?


Solution

  • Simple: When the song changes, set a variable foo somewhere to the current time, and set another variable bar to the new song's current playback time (usually gonna be 0.0.) When the song changes again, subtract the value of foo from the current time, then add the value of bar; the result is the final offset of the song that was just being played.

    You'll also have to handle fast forward, rewind, stop, and pause, but you can do that by monitoring the playback state and querying the song's current playback time to keep yourself in sync as needed.