如何阻止MPMediaPickerController用重复的MPMediaItems填充MPMediaItemCollection?

时间:2013-04-25 00:48:46

标签: 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 import";
//we want to be able to import multiple items at once
mediaPickerController.allowsPickingMultipleItems = YES;
mediaPickerController.delegate = self;

选择MPMediaPickerController中的项目时,我注意到同一项目按两次会将其添加到MPMediaItemCollection两次。当我将曲目导入我的应用程序时,我最终导入它们两次。

我想要一种方法来指定轨道只被添加一次,或者,如果没有,则能够从选择中过滤掉重复的轨道。如果这是可能的,我将如何去做呢?

我的目标是iOS5。

2 个答案:

答案 0 :(得分:2)

我在一段时间后遇到了这个问题,不幸的是,如果没有从头开始重新构建选择器(可行),则无法指定是否允许重复。但是,选择后检查和删除重复项是相对轻松的。

下面列出了几个选项,但差异实际上归结为您是否希望保留该集合最初的订单。

第一个基本上只循环遍历集合中的项目,如果辅助数组不包含该对象,则将其复制出来。第二个将集合中的所有对象复制到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电缆,但它应该足以让您前进。

答案 1 :(得分:0)

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