Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift | macOS | Retrieving track title and artist of currently playing music

Tags:

macos

swift

I've embarked on a mini project to try and learn to write Swift. I thought it would be useful to write a MacOS app that would display currently playing track title and artist info in the menu bar. At the moment I have to open the app in question, whether that be Spotify, Apple's Music or Tidal, which disrupts workflow.

Given that the new-ish "Now Playing" notification widget displays this already (regardless of the source of audio), I figured it would be fairly simple to do something similar for the menu bar.

I have researched thoroughly and read related yet very dated questions, which suggest the only now playing info/data available is from iTunes, however it looks like things have evolved since then, especially since iTunes is to be killed off, so I'm re-asking and hoping things are more global in 2020.

With regard to code I've discovered nowPlayingInfo which suggests it might do what I'm after but I've spent 4 hours trying to get it to work without success. I've also looked at MPNowPlayingCenter but, similarly, had no result.

There seems to be lots of help regarding setting Now Playing info, but very little regarding retrieving/getting it.

So far I've managed to write the code to add text to the menu bar:

    func applicationDidFinishLaunching(_ aNotification: Notification) {

        let statusBar = NSStatusBar.system
        statusBarItem = statusBar.statusItem(
            withLength: NSStatusItem.variableLength)
        statusBarItem.button?.title = "This should be a string of the track name and artist"

    }

I've also gathered snippets of potentially useful code:

        // var nowPlayingInfo: [String : Any](? {})
        // let MPMediaItemPropertyTitle: String
        // let MPMediaItemPropertyArtist: String

        // static let MPMusicPlayerControllerNowPlayingItemDidChange: NSNotification.Name
        // MPNowPlayingPlaybackState

        // class var applicationMusicPlayer: MPMusicPlayerController { get }

        // class MPNowPlayingInfoCenter : NSObject
        // let nowPlaying = MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
        // let trackName = nowPlaying.valueForProperty(MPMediaItemPropertyTitle) as String

After half-a-day of trial-and-error, with Xcode warning me of errors, and lots of research I'm turning to the experts on stack overflow for some guidance.

like image 563
morgyface Avatar asked Sep 12 '25 22:09

morgyface


1 Answers

I can't answer the question completely since I'm not a Swift developer. However, I can offer some insights since I'm thinking about a similar project to learn Swift.

  1. There is systemMusicPlayer in MusicKit's MPMusicPlayerController. The documentation says that it "returns the system music player, which controls the Music app’s state". It has a nowPlayingItem property which may be what you need.

  2. There is a 3rd-party app called Emcee that displays the currently-playing track info. The website mentions iTunes but I can confirm that it works with Music app in Catalina, so retrieving currently-playing track is certainly possible. It even displays the star rating of the song, see the screenshot. (I'm new to SO so can't embed images in answers yet, sorry.) During installation, this app asked my permission to access Music app, so I guess it uses official Apple Music APIs.

Hope this helps somewhat, and good luck with your project! Please post back if you'll be successful in your task.

like image 119
Oleg Podchashynskyi Avatar answered Sep 15 '25 15:09

Oleg Podchashynskyi