我已经成功创建了一个可以(几乎)完美运行的下一个排队系统。我可以移动,删除和添加歌曲到列表,没有任何问题。我遇到的唯一问题是,当用户决定使用随机播放模式(例如MPMusicShuffleMode
。歌曲)时,上一个视图仍会显示旧顺序(假设随机播放模式设置为关闭)。应用程序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
}
答案 0 :(得分:0)
所以在伪代码中它看起来像下面这样。
由于