代码之家  ›  专栏  ›  技术社区  ›  Aran Mulholland JohnnyAce

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

  •  1
  • Aran Mulholland JohnnyAce  · 技术社区  · 12 年前

    我用这个代码来选择曲目

    //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 回复  |  直到 12 年前
        1
  •  2
  •   Mick MacCallum    12 年前

    不久前我遇到了这个问题,不幸的是,如果不从头开始重新制作拾取器(可行),就无法指定是否允许重复。然而,在选择后检查并删除重复项相对来说是无痛的。

    下面列出了几个选项,但区别实际上归结为您是否希望保留集合最初的顺序。

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

        2
  •  0
  •   Ishwar Hingu    6 年前
            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()