使用 'Shuffle Albums' 为下一个队列(MediaPlayer)随机播放 NSMutableArray?

Shuffle NSMutableArray with 'Shuffle Albums' for up next queue (MediaPlayer)?

我已经设法创建了一个(几乎)完美运行的下一个排队系统。我可以毫无问题地移动、删除和添加歌曲到列表中。我遇到的唯一问题是,当用户决定使用随机播放模式(例如 MPMusicShuffleMode.Songs)时,下一个视图仍然显示旧顺序(假设随机播放模式设置为关闭)。应用程序 Ecoute 具有类似的排队系统,但在洗牌时也可以使用。

我发现 Ecoute 不使用 MPMusicShuffleMode,如果您在 Ecoute 中按随机播放后进入股票音乐播放器,它保持不变。我想出了一种方法来显示随机播放歌曲的正确顺序(只需随机播放 NSMutableArray 并将其设置为新队列)。真正的问题是当用户想要 'Shuffle Albums' 时,我将如何洗牌这个数组?如果有帮助,NSMutableArray 包含 MPMediaItem

非常感谢。

编辑:

这种尝试可行,但时间会随着歌曲的数量呈指数级增长。 2000 首歌曲大约需要 25-35 秒,非常慢。

else if(self.shuffleMode == "Songs"){
        self.shuffleMode = "Albums"
        self.shuffle.setTitle("Shuffle Albums", forState: UIControlState.Normal)
        var queueCopy = NSMutableArray(array: self.queue.items)
        var newQueue = NSMutableArray(array: [])

        var albums = MPMediaQuery.albumsQuery().collections as [MPMediaItemCollection]
        var albumsCopy = NSMutableArray(array: albums)
        shuffleArray(albumsCopy)

        for album in albumsCopy{
            for item in queueCopy{
                if (album.representativeItem!.valueForProperty(MPMediaItemPropertyAlbumTitle) as NSString == item.valueForProperty(MPMediaItemPropertyAlbumTitle) as NSString){
                    for(var i = 0; i < album.items!.count; i++){
                        if(album.items![i].valueForProperty(MPMediaItemPropertyTitle) as NSString == item.valueForProperty(MPMediaItemPropertyTitle) as NSString){
                            newQueue.addObject(item as MPMediaItem)
                        }
                    }
                }
            }
        }

        let newQueueToSet = MPMediaItemCollection(items: newQueue)
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        appDelegate.currentQueue = newQueueToSet
        self.queue = newQueueToSet

        self.player.setQueueWithItemCollection(newQueueToSet)

}

编辑 2:

设法将其降低到 8 秒,但与 Ecoute 的 1 到 2 秒相去甚远。
8 第二个版本:

        var albumNames = [String]()
        for item in queueCopy{
            albumNames.append(item.valueForProperty(MPMediaItemPropertyAlbumTitle) as NSString)
        }

        let unique = NSSet(array: albumNames).allObjects as [MPMediaItem]
        var uniqueAlbumNames = NSMutableArray(array: unique)
        shuffleArray(uniqueAlbumNames)

        for name in uniqueAlbumNames{
            for item in queueCopy{
                if item.valueForProperty(MPMediaItemPropertyAlbumTitle) as NSString == name as NSString{
                    newQueue.addObject(item as MPMediaItem)
            }
        }
}

最终编辑:

我坚持使用的最后一段代码。近3000首歌曲大约需要7-8秒。

    @IBAction func shufflePressed(sender: AnyObject) {

    if self.shuffleMode == "Off"{
        self.shuffleMode = "Songs"
        self.shuffle.setTitle("Shuffle All", forState: UIControlState.Normal)
        self.shuffle.setTitle("Shuffling", forState: UIControlState.Highlighted)


        var queueCopy = NSMutableArray(array: self.queue.items)
        self.unshuffledQueueCopy = self.queue

        shuffleArray(queueCopy)
        let newQueue = MPMediaItemCollection(items: queueCopy)
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        appDelegate.currentQueue = newQueue
        self.queue = newQueue

        self.player.setQueueWithItemCollection(newQueue)

    }
    else if(self.shuffleMode == "Songs"){
        self.shuffleMode = "Albums"
        self.shuffle.setTitle("Shuffle Albums", forState: UIControlState.Normal)

        var queueCopy = NSMutableArray(array: self.queue.items)
        var newQueue = NSMutableArray(array: [])

        var albums = MPMediaQuery.albumsQuery().collections as [MPMediaItemCollection]
        var albumsCopy = NSMutableArray(array: albums)
        shuffleArray(albumsCopy)


        // Takes 8 seconds for 3000 songs

        var albumNames = [String]()

        for item in queueCopy{
            albumNames.append(item.valueForProperty(MPMediaItemPropertyAlbumTitle) as NSString)
        }

        let unique = NSSet(array: albumNames).allObjects as [MPMediaItem]
        var uniqueAlbumNames = NSMutableArray(array: unique)
        shuffleArray(uniqueAlbumNames)


        for name in uniqueAlbumNames{
            for item in queueCopy{
                if item.valueForProperty(MPMediaItemPropertyAlbumTitle) as NSString == name as NSString{
                    newQueue.addObject(item as MPMediaItem)
                }
            }
        }

        let newQueueToSet = MPMediaItemCollection(items: newQueue)
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        appDelegate.currentQueue = newQueueToSet
        self.queue = newQueueToSet

        self.player.setQueueWithItemCollection(newQueueToSet)

    }
    else if(self.shuffleMode == "Albums"){
        self.shuffleMode = "Off"
        self.shuffle.setTitle("Shuffle", forState: UIControlState.Normal)
        var queueCopy = NSMutableArray(array: self.unshuffledQueueCopy.items)
        var nowPlayingItem = self.player.nowPlayingItem
        for(var i = 0; i < queueCopy.count; i++){
            if(queueCopy[i] as MPMediaItem == nowPlayingItem){
                self.fromIndex = i
            }
        }
        let newQueue = MPMediaItemCollection(items: queueCopy)
        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        appDelegate.currentQueue = newQueue
        self.queue = newQueue

        self.player.setQueueWithItemCollection(newQueue)
        self.fromItem = newQueue.items[fromIndex + 1] as MPMediaItem

    }

所以在伪代码中它看起来像下面这样。

  1. 循环数组,只创建新的专辑数组
  2. 随机播放新相册数组
  3. 循环新相册数组,在原始数组中查找该相册中的项目。
  4. 比赛时,将这些歌曲写入第三个数组,这将成为您的新播放列表。

谢谢