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



我设法创建了一个完美工作(几乎)的下一个排队系统。我可以在没有任何问题的情况下移动,删除并将歌曲添加到列表中。我唯一的问题是,当用户决定使用混音模式(例如 MPMusicShuffleMode .songs)时,下一个视图仍然显示旧顺序(假设洗牌模式设置为关闭)。该应用程序Ecoute具有类似的排队系统,但是在改组时也可以工作。

我已经意识到Ecoute不使用MPMusicShuffleMode,如果您在Ecoute中按Shuffle后进入股票音乐播放器,则它保持不变。我已经找到了一种显示正确的订购歌曲的顺序(只需洗牌NSMutableArray并将其设置为新队列)。真正的问题是,当用户想"随机散装专辑"时,我将如何洗牌?如果有帮助,则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. 在比赛中,将这些歌曲写入将成为您的新播放列表的第三个数组。

谢谢

相关内容

  • 没有找到相关文章

最新更新