4

In my app I'm checking if the currently playing song is part of an album. I am able to do this but I have found that if an album is part of a compilation my code does not work as expected.

What I want to do is if the currently playing song is part of an album a button becomes enabled and if it is not the button is not enabled. There is an ablum on my iTunes where all of the songs have the exact same Album name but on the app the button does not enable, but iTunes marked the album as a Compilation.

The code I am using to query is

let albumPredicate = MPMediaPropertyPredicate(value: item.albumTitle, forProperty: MPMediaItemPropertyAlbumTitle)

I have tried

let compilationPredicate = MPMediaPropertyPredicate(value: item.isCompilation, forProperty: MPMediaItemPropertyIsCompilation)

and later

if item.isCompilation == true to determine if I would add this predicate to the filter set.

I also need to be careful that I am not accidentally disabling the button because the currently playing song is not part of a compilation as a song can be part of an album but not a compilation as well as be in an album and compilation.

func getSongsWithCurrentAlbumFor(item: MPMediaItem) -> MPMediaQuery {

  let artistPredicateWithin = MPMediaPropertyPredicate(value: item.artist, forProperty: MPMediaItemPropertyArtist, comparisonType: .contains)
  let albumPredicate = MPMediaPropertyPredicate(value: item.albumTitle, forProperty: MPMediaItemPropertyAlbumTitle)

  let compilationPredicate = MPMediaPropertyPredicate(value: item.isCompilation, forProperty: MPMediaItemPropertyIsCompilation)

  var isComp: String
  var myFilterSet: Set<MPMediaPropertyPredicate>!

  if item.isCompilation == true {
    myFilterSet = [artistPredicateWithin, albumPredicate, compilationPredicate]
    isComp = "true"
  } else {
    myFilterSet = [artistPredicateWithin, albumPredicate]
    isComp = "false"
  }

  print(isComp)

  var query = MPMediaQuery(filterPredicates: nil)
  query = MPMediaQuery(filterPredicates: myFilterSet)
  query.addFilterPredicate(albumPredicate)
  albumQuery = query
  return albumQuery
}

What have I done wrong in my code?

RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70

1 Answers1

0

The documentation for the MPMediaItemPropertyIsCompilation property states it should be wrapped in an NSNumber value.

okoroezenwa
  • 23
  • 1
  • 6