Search code examples
iosvideoalassetslibrary

play all videos from gallery ios


i want to play all videos from gallery directly, but in this code last video is played every time, what should i do to play all videos in a sequence one by one

 ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            [group setAssetsFilter:[ALAssetsFilter allVideos]];
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){

                if (asset)
                {

                    NSDictionary *meta = [[asset defaultRepresentation] metadata];

                    if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                        // asset is a video
                        NSLog(@"See Asset: %@", asset);

                        self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[[asset defaultRepresentation] url]];
                        if (self.moviePlayer != nil)
                        {
                            [self stopPlayingVideo:nil];
                        }
                        if (self.moviePlayer != nil)
                        {
                            [[NSNotificationCenter defaultCenter]
                             addObserver:self
                             selector:@selector(videoHasFinishedPlaying:)
                             name:MPMoviePlayerPlaybackDidFinishNotification
                             object:self.moviePlayer];
                            NSLog(@"Successfully instantiated the movie player.");
                            /* Scale the movie player to fit the aspect ratio */
                            self.moviePlayer.scalingMode = MPMovieScalingModeNone;
                            /* Let's start playing the video in full screen mode */
                            self.moviePlayer.view.frame = CGRectMake(100, 50, 650, 500);
                            [self.moviePlayer play];
                            //        [self.moviePlayer.view setFrame:self.view.bounds];
                            [self.view addSubview:self.moviePlayer.view];
                            [self.moviePlayer setFullscreen:NO
                                                   animated:YES];

                            volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-410, 300, 140, 52)] ;

                            [self.view addSubview:volumeView];


                            CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
                            volumeView.transform = trans;


                            for (id current in volumeView.subviews) {
                                if ([current isKindOfClass:[UISlider class]]) {
                                    volumeSlider = (UISlider *)current;

                                    volumeSlider.minimumTrackTintColor = [UIColor grayColor];
                                    volumeSlider.maximumTrackTintColor = [UIColor whiteColor];


                                    volumeSlider.value = volumeSlider.maximumValue;
                                    [btnVol setBackgroundImage:[UIImage imageNamed:@"mx_vol.png"] forState:UIControlStateNormal];

                                }
                            }


                            [self.moviePlayer setControlStyle:MPMovieControlStyleNone];
                        } else {
                            NSLog(@"Failed to instantiate the movie player.");
                        }
                        [_moviePlayer setRepeatMode:MPMovieRepeatModeOne];

                    }

                    //                    NSLog(@"%@",asset);
                                        NSLog(@"%@",meta);

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

Solution

  • You are playing video inside the enumeration block (It's a looping thing, it loops through all your video and stops at last one, that's why it is playing the last one only).

    You need to retrieve the url of videos first:

    NSMutableArray  *videos  = [[NSMutableArray alloc] init];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {
         if (group)
         {
             [group setAssetsFilter:[ALAssetsFilter allVideos]];
             [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
              {
                  if (asset)
                  {
                      NSURL  *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
                      [videos addObject:videoURL];
                  }
              }];
          }
     }
      failureBlock:^(NSError *error)
     {
         NSLog(@"error enumerating AssetLibrary groups %@\n", error);
     }];
    

    Now you need to play the first video and listen the MPMoviePlayerPlaybackDidFinishNotification and start playing next video.