【问题标题】:How do I stop the MPMediaPickerController filling MPMediaItemCollection with duplicate MPMediaItems?如何停止 MPMediaPickerController 用重复的 MPMediaItems 填充 MPMediaItemCollection?
【发布时间】:2013-04-25 00:48:45
【问题描述】:

我正在使用此代码选择曲目

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

【问题讨论】:

    标签: ios mpmediaitem mpmediapickercontroller mpmediaitemcollection


    【解决方案1】:

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

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

    第一个基本上只是循环遍历集合中的项目,如果辅助数组不包含该对象,则将其复制出来。第二个将集合中的所有对象复制到一个 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】:
              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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        • 2013-02-25
        • 1970-01-01
        • 2014-09-14
        • 1970-01-01
        相关资源
        最近更新 更多