rryam / MusadoraKit

The ultimate companion to MusicKit.
http://exploringmusickit.com
MIT License
344 stars 22 forks source link

Search for songs using their Persistent ID #37

Closed aviwad closed 7 months ago

aviwad commented 7 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

For my use case I receive updates from either Apple Music's internal "UserNotfications" or via AppleScript to get the currently playing song's ID: its "Persistent ID" in this case. However for my uses I want a musickit object representing this song (I need its isrc to get the equivalent Spotify ID.... I have a lyrics player). However this is currently not possible.

rudrankriyam commented 7 months ago

@aviwad you will have to use MediaPlayer framework as well. Here is a working code where you give it the persistent ID and get the Song object that contains the ISRC using MusadoraKit. Hope it helps!

let predicate = MPMediaPropertyPredicate(value: "persistentID", forProperty: MPMediaItemPropertyPersistentID)
let songQuery = MPMediaQuery()
songQuery.addFilterPredicate(predicate)

if let items = songQuery.items, items.count > 0 {
  var song = items[0]
  print(song?.title)
  print(song?.artist)

  let catalogID = song?.playbackStoreID ?? ""

  let catalogSong = try await MCatalog.song(id: MusicItemID(catalogID))
  print(catalogSong)
  print(catalogSong.isrc)
}
aviwad commented 7 months ago

Hi. My program runs on macOS (AppleScript and UserNotifications are only on macOS). Some components of the Media Player framework such as MPMediaPropertyPredicate and MPMediaQuery are unavailable on macOS. Is there any other solution?

rudrankriyam commented 7 months ago

Ah, I overlooked that detail. I really do not know any other way

aviwad commented 7 months ago

No worries. I found a solution: use a private api and grab the store ID of the currently playing song through it https://stackoverflow.com/questions/61003379/how-to-get-currently-playing-song-on-mac-swift