我已经成功地创建了一个上下一个排队系统,它工作得几乎完美。我可以移动,删除和添加歌曲到列表中没有任何问题我遇到的唯一问题是,当用户决定使用洗牌模式(例如MPMusicShuffleMode
.songs)时,“上下一个”视图仍然显示旧顺序(假设洗牌模式设置为“关闭”)。应用程序Ecoute有一个类似的排队系统,但它也可以在洗牌时工作。
我意识到ecoute不使用MPMusicShuffleMode
,如果在ecoute中按shuffle后进入stock music player,它将保持不变。我已经找到了一种显示无序歌曲正确顺序的方法(只需无序播放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
}
最佳答案
所以在伪代码中,它看起来像下面这样。
循环数组,仅生成相册的新数组
洗牌新相册数组
循环新相册数组,在原始数组中查找该相册中的项目。
在match上,将这些歌曲写入第三个数组,该数组将成为您的新播放列表。
谢谢