How to list all videos from user device

115 Views Asked by At

I want to create a custom library where i will get all videos with their folder name. I implemented some code but this is not working properly. My code get videos from some folders but also drop some folder. i want to fetch all folders.

This is my code:-

 -(void)getAllVideos
{
 assetItems = [NSMutableArray arrayWithCapacity:0];
allVideos = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
 {
     if (group)

     {
         NSString * str = [NSString stringWithFormat:@"%@",group];
         int power = [str rangeOfString:@","].location;
         NSString *str1= [str substringToIndex:power];

         int dash = [str rangeOfString:@":"].location;
         NSString *final = [str1 substringFromIndex:dash+1];

         combineArray =[[NSMutableArray alloc]init];
         thumbnailImages = [[NSMutableArray alloc]init];
         videosURL = [[NSMutableArray alloc]init];

         NSLog(@"name of folder%@",group);
         [groupName addObject:final];

         [group setAssetsFilter:[ALAssetsFilter allVideos]];
         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
          {

              if (asset)
              {
                  dic = [[NSMutableDictionary alloc] init];
                  ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
                  NSString *uti = [defaultRepresentation UTI];
                  NSURL  *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
                  NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];


                  AVAsset *asset = [AVAsset assetWithURL:videoURL];
                  AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
                  CMTime time = [asset duration];
                  time.value = 0;
                  CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
                  UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
                  CGImageRelease(imageRef);



                  [thumbnailImages addObject:thumbnail];
                  [videosURL addObject:videoURL];




              }

          } ];
         if(thumbnailImages.count >0)
         [combineArray addObject:thumbnailImages];

         if(videosURL.count > 0)
         [combineArray addObject:videosURL];
         if(combineArray.count>0)
         [alldata setObject:combineArray forKey:final];
     }
     // group == nil signals we are done iterating.
     else
     {
         dispatch_async(dispatch_get_main_queue(), ^{

         });
     }
 }
                          failureBlock:^(NSError *error)
 {
     NSLog(@"error enumerating AssetLibrary groups %@\n", error);
 }];




}

Please suggest me what changes i have to do?

0

There are 0 best solutions below