Ios 如何停止MPMediaPickerController使用重复的MPMediaItems填充MPMediaItemCollection?
我用这个代码来选择曲目Ios 如何停止MPMediaPickerController使用重复的MPMediaItems填充MPMediaItemCollection?,ios,mpmediaitem,mpmediapickercontroller,mpmediaitemcollection,Ios,Mpmediaitem,Mpmediapickercontroller,Mpmediaitemcollection,我用这个代码来选择曲目 //open the media picker, allow the inport of any type of audio MPMediaPickerController *mediaPickerController = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio]; mediaPickerController.prompt = @"Choose items to i
//open the media picker, allow the inport of any type of audio
MPMediaPickerController *mediaPickerController = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];
mediaPickerController.prompt = @"Choose items to import";
//we want to be able to import multiple items at once
mediaPickerController.allowsPickingMultipleItems = YES;
mediaPickerController.delegate = self;
在MPMediaPickerController
中选择项目时,我注意到两次按同一项目会将其添加到MPMediaItemCollection
中。当我将曲目导入我的应用程序时,我最终将它们导入了两次
我想要一种方法来指定只添加一次轨迹,或者,如果添加失败,可以从选择中筛选出重复的轨迹。如果这是可能的,我该怎么做呢
我的目标是iOS5。不久前我遇到了这个问题,不幸的是,如果不从头开始重新创建选择器(do-able),就无法指定是否允许重复。但是,在选择后检查并删除重复项相对来说比较轻松 有两个选项,如下所述,但区别实际上归结为您是否希望保留集合最初的顺序 第一个基本上只是循环遍历集合中的项,如果辅助数组不包含该对象,它会将其复制出来。第二种方法将集合中的所有对象复制到NSSet中,NSSet将删除所有重复项,然后将其复制回来。这种解决方案根本无法维持秩序
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
MPMediaItemCollection *dupeFreeCollection = nil;
if (shouldPreserveOrder) {
//Will preserve order, with exception of subtracted duplicates
NSMutableArray *newCopy = [NSMutableArray new];
for (MPMediaItem *item in mediaItemCollection.items) {
if (![newCopy containsObject:(MPMediaItem *)item]) {
[newCopy addObject:(MPMediaItem *)item];
}
}
dupeFreeCollection = [[MPMediaItemCollection alloc] initWithItems:newCopy];
}else{
//Will not preserve order
NSSet *set = [[NSSet alloc] initWithArray:mediaItemCollection.items];
dupeFreeCollection = [[MPMediaItemCollection alloc] initWithItems:[set allObjects]];
}
}
请记住,我包含的代码未经测试,因为我似乎找不到iPhone电缆,但它应该足以让您继续使用。in swift
In swift
for i in 0 ..< mediaItemCollection.items.count {
let representativeItem: MPMediaItem = mediaItemCollection.items[i]
let title = representativeItem.title
let artist = representativeItem.artist
let imageSound: MPMediaItemArtwork = (representativeItem.value( forProperty: MPMediaItemPropertyArtwork ) as? MPMediaItemArtwork)!
let itemUrl = representativeItem.value(forProperty: MPMediaItemPropertyAssetURL) as? NSURL
SongDict = NSMutableDictionary()
self.setSongData(title, artist: artist, img: imageSound.image(at: CGSize(width: 55, height: 55))!, url:itemUrl!)
songArray .insert(SongDict, at: i)
}
func setSongData(_ title: String?, artist: String?, img :UIImage , url: NSURL?) {
SongDict["title"] = title
SongDict["artist"] = artist
SongDict["image"] = img
SongDict["url"] = url
}
Now you can delete below code
let indexPath = IndexPath(row: sender.tag, section: 0)
self.tbl_Song.beginUpdates()
self.songArray.removeObject(at: indexPath.row)
self.tbl_Song.deleteRows(at: [indexPath], with: .left)
self.tbl_Song.endUpdates()
对于0中的i..