Как я могу удалить дубликаты из MPMediaQuery? Усилие ниже имеет фатальную ошибку - PullRequest
2 голосов
/ 19 июня 2019

Я создаю приложение Music Player. MPMediaQuery возвращает дубликаты. Например, когда я получаю список песен из определенного альбома (10 песен находятся на самом CD), 1 песня появляется дважды. Я не знаю почему. Может быть, я сэмплировал свой старый CD для первой песни, а затем в какой-то момент я сэмплировал весь CD. Я не знаю. В любом случае, мое приложение и музыкальное приложение Apple дважды перечисляют песню, поэтому, когда я играю в альбом, я слышу эту песню дважды. Я хотел бы устранить это. Кстати, как и следовало ожидать, существует 11 различных идентификаторов PersistentID (один не в порядке).

Я потратил кучу времени на изменение кода и поиск переполнения стека.

// Filter the Albums by the Artist (PASSED in from the previous View Controller)

let predicateByArtist = MPMediaPropertyPredicate(value: selectedArtist, forProperty: MPMediaItemPropertyArtist)
// Filter the Songs by the AlbumTitle (PASSED in from the previous View Controller)
let predicateByAlbumTitle = MPMediaPropertyPredicate(value: selectedAlbumTitle, forProperty: MPMediaItemPropertyAlbumTitle)
// Query the songs
qrySongs = MPMediaQuery.songs()
qrySongs.addFilterPredicate(predicateByArtist)
qrySongs.addFilterPredicate(predicateByAlbumTitle)

//********** June 19, 2019 additional effort...
// At this point, qrySongs has 11 songs (note: the album City to City really only has 10 songs
// In an effort to learn Xcode/Swift, I thought would if I made a new array of MPMediaItems that only had the list of unique song titles (I'm trying to skip the one duplicate).  Then I could use this list: qrySongsUnique when I assign titles to the TableView.  The theory being that the MPMusicPlayerController.systemMusicPlayer is really just playing a PersistentID.
// Maybe this idea will not work, maybe it will (but should only be used with small arrays like albums) ... but I'm confused as to why I'm getting a fatal error...

let count = qrySongs.items?.count
print(count!)  // Optional(11)
var i: Int = 0
var lastRowItemTitle: String = ""
var qrySongsUnique: [MPMediaItem] = [MPMediaItem]()

if count ?? 0 > 0 {
    while i < count ?? 0 {
        print(i)  // 0
        let currentRowItemTitle = qrySongs.items![i].title
        print(currentRowItemTitle!)  // Optional("The Ark")
        if currentRowItemTitle != lastRowItemTitle {
            print(qrySongs.items![i]) // <MPConcreteMediaItem: 0x280f4da10> 6027817404062467305
            qrySongsUnique[i] = qrySongs.items![i]  // Thread 1: Fatal error: Index out of range
            lastRowItemTitle = currentRowItemTitle ?? "skipped"
        }
        i = i + 1
    }
}
//**********

Список уникальных названий песен

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...