2

I am writing small app that can play music. To get all songs I am using code below

 var songsArray: [MPMediaItem] = [MPMediaItem]()
 var mediaQuery = MPMediaQuery()
 songsArray = MPMediaQuery.songsQuery().items as [MPMediaItem]

 for songItem in songsArray {            
     var song: myMusicItem? = myMusicItem(songTitle: songItem.title, songAlbum: songItem.albumTitle, songArtist: songItem.albumArtist, songGenre: songItem.genre)

     retArray.append(song!)
 }

 println(String(format:"Number of songs:\t%i", retArray.count))

And the problem is that retArray has more items than actually is in my library. Does anyone have an idea why this situation is happening?

metal_man
  • 580
  • 1
  • 9
  • 22
  • What you mean by more Item ? Is that array contains duplicate items ? How you are calling this method and from where ? – Midhun MP Mar 18 '15 at 06:33
  • More items means if my music library have 900 songs, `MPMediaQuery.songsQuery().items as [MPMediaItem]` returns more than 1000 items. I don't have duplicated items.Method with this code is called on button touch. And array does not contains duplicate items. – metal_man Mar 18 '15 at 06:57
  • 3
    Maybe videos? "A media item represents a single piece of media (such as one song or one video podcast) in the iPod library." https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMediaItem_ClassReference/#//apple_ref/c/data/MPMediaItemPropertyPersistentID – iOSfleer Mar 18 '15 at 07:35
  • Well... but I am using MPMediaQuery.songsQuery() so I think only songs should be taken, not videos or podcasts. I have one more idea - what about songs in cloud? In settings I set option to not to show items that are in cloud only. Can this be it? – metal_man Mar 18 '15 at 07:50

1 Answers1

0

As you suggested in the comments, iCloud media items, which are included by default, could be the difference.

Try adding this filter predicate to your query to not include iCloud items:

query.addFilterPredicate(MPMediaPropertyPredicate(value: false, forProperty: MPMediaItemPropertyIsCloudItem))
Ric Santos
  • 15,419
  • 6
  • 50
  • 75