vendredi 8 mai 2015

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

I have managed to create an up next queuing system that works (almost) perfectly. I can move, delete and add songs to the list without any issues at all. The only problem i am having is that when the user decides to use a shuffle mode (e.g. MPMusicShuffleMode.Songs), the up next view still displays the old order (assumes shuffle mode is set to off). The app Ecoute has a similar queuing system but it also works when shuffling.

I've realized that Ecoute does not use the MPMusicShuffleMode, if you go into the stock music player after pressing shuffle in Ecoute, it stays the same. I've figured out a way to display the correct order for shuffled songs (just shuffle the NSMutableArray and set this as the new queue). The real problem is how would i shuffle this array when the user wants to 'Shuffle Albums'? The NSMutableArray contains MPMediaItem if that helps.

Many thanks.

EDIT:

This attempt works but the time grows exponentially for the number of songs. 2000 songs took about 25-35 seconds, so very slow.

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)

}

EDIT 2:

Managed to get it down to 8 seconds but it's nowhere near Ecoute's 1 to 2 seconds.
8 second version:

        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)
            }
        }
}

Final Edit:

final piece of code that i'm sticking with. takes about 7-8 seconds for nearly 3000 songs.

    @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

    }

Aucun commentaire:

Enregistrer un commentaire