1

我使用此代码选择曲目

//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。

4

2 回答 2

2

不久前我遇到了这个问题,不幸的是,如果不从头开始重新制作选择器(可行),就无法指定是否允许重复。但是,选择后检查和删除重复项相对容易。

如下所述,有几个选项,但区别实际上归结为您是否希望保留集合最初的顺序。

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

于 2013-04-25T03:15:05.780 回答
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()
于 2018-10-24T12:19:58.257 回答