Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
How to know when MPMusicPlayerController changes playing item naturally

I am using the MPMusicPlayerController to create a music player in my app. I have got it all working great except for one small issue:

When the songs changes naturally - one song finishes and the next starts from the set queue - the notification MPMusicPlayerControllerNowPlayingItemDidChange doesn't appear to be called.

At the moment I am utilising both the MPMusicPlayerControllerNowPlayingItemDidChange and MPMusicPlayerControllerPlaybackStateDidChange notifications. These cover playing, pausing, shuffle, repeat, next, previous etc. When the notifications are hit I then refresh the screen based on the MPMusicPlayerController to show the new song, artist or different button icons required. Neither of these are called though when a song finishes and the next one automatically starts playing - this means that the title and artist of the previous song is left until the user reloads the screen or interacts with the audio controls which is not good user experience.

Short of regularly checking whether the current name matches the playing name I don't know how to update this in the normal flow of the app.

NotificationCenter.default.addObserver(
  forName: NSNotification.Name.MPMusicPlayerControllerNowPlayingItemDidChange,
  object: musicPlayerController,
  queue: nil) { _ in
    // Update view
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The answer to this turns out to be very simple but also difficult to spot if you're not looking in the right place.

Before we add our observers we need to begin generating the playback notifications:

musicPlayerController.beginGeneratingPlaybackNotifications()

NotificationCenter.default.addObserver(self,
                                       selector: #selector(refreshView),
                                       name: .MPMusicPlayerControllerPlaybackStateDidChange,
                                       object: musicPlayerController)

NotificationCenter.default.addObserver(self,
                                       selector: #selector(refreshView),
                                       name: .MPMusicPlayerControllerNowPlayingItemDidChange,
                                       object: musicPlayerController)

We also need to remember to end generating them when we leave (deallocate) the view:

deinit {
    NotificationCenter.default.removeObserver(self, name: .MPMusicPlayerControllerPlaybackStateDidChange, object: nil)
    NotificationCenter.default.removeObserver(self, name: .MPMusicPlayerControllerNowPlayingItemDidChange, object: nil)
    musicPlayerController.endGeneratingPlaybackNotifications()
}

The confusion came from the musicMediaPlayer returning a number of notifications even without this which didn't point to the fact we weren't observing all the notifications that were being fired.

Note: It is worth noting that as of the time of writing this it was in discussion whether there was a need to manually remove observers - I have included it here for answer completeness.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!