0

I am playing an video from document directory in the collection view using below code

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{

        if (indexPath.item==0) {

        NSURL *vedioURL;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

        NSLog(@"files array %@", filePathsArray);

        //reverse the array
        //    NSArray* reversedArray = [[filePathsArray reverseObjectEnumerator] allObjects];
        //    NSLog(@"%@",reversedArray);

        NSString *fullpath;

        for ( NSString *apath in filePathsArray )
        {
            fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
            vedioURL =[NSURL fileURLWithPath:fullpath];
        }
        NSLog(@"vurl %@",vedioURL);

        MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
        videoPlayerView.view.frame = CGRectMake(0, 60, 412,229);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
        [view addSubview:videoPlayerView.view];
        [self.view addSubview:view];
        [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
        [videoPlayerView.moviePlayer play];


    }

When I click on the image video is playing,but I want that video to be played in same position in the same cell.

4 Answers4

0
    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
    videoPlayerView.view.frame = CGRectMake(0, 60, 412,229);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    [view addSubview:videoPlayerView.view];
    [self.view addSubview:view];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];
    [view setClipsToBounds:YES];
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
0
 - (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
 {

@try {


    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    // A safe check before going to access sibview.
    if ([cell.contentView.subviews count] > 0) 
    {
      // Find your imageview here
    }
}
@catch (NSException *exception) {
    NSLog(@"Exception :%@",exception.debugDescription);
}
}
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
  • Hi Bro sorry for the delay this MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; [view addSubview:videoPlayerView.view]; [self.view addSubview:view]; [self presentMoviePlayerViewControllerAnimated:videoPlayerView]; [videoPlayerView.moviePlayer play]; [view setClipsToBounds:YES]; in the collection view cell now when i click the image the video have to play – Satheeshkumar Naidu Aug 09 '16 at 07:29
  • The add a UIGesture to the image and detect the tap. Give every image a unique id and when the UIGesture responds there add your play method – gurmandeep Aug 09 '16 at 08:42
  • one doubt is I will add images randomly in that 3 image one is video first frame image when ever notification center fires one image will add to the collection view cell if that image is added to the collection view cell the video have to play in the cell,how to do that it same like instagram recording video and add it to the timeline – Satheeshkumar Naidu Aug 09 '16 at 08:50
  • On which image you want to tap? – gurmandeep Aug 09 '16 at 08:54
  • video first frame image,but all the images is adding to the same array – Satheeshkumar Naidu Aug 09 '16 at 09:00
  • Do one things, add a tap gesture on the video thumbnail and get the responder differently. Forget about didselect – gurmandeep Aug 09 '16 at 09:02
  • dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; [assetLibrary writeVideoAtPathToSavedPhotosAlbum:recordedFile completionBlock: ^(NSURL *assetURL, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error != nil) { } else { title = @"Saved!"; message = nil;}{save alert NSLog(@"%@",recordedFile); – Satheeshkumar Naidu Aug 09 '16 at 09:07
  • i am recording the video the output is in the form of url and i am saving it – Satheeshkumar Naidu Aug 09 '16 at 09:08
  • How to do that bro – Satheeshkumar Naidu Aug 09 '16 at 09:08
0

Tap gesture

UITapGestureRecognizer *singleFingerTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self 
                                        action:@selector(handleSingleTap:)];
singleFingerTap.delegate = self;
[yourimageview addGestureRecognizer:singleFingerTap];

Try the following code:

conform the <UIGestureRecognizerDelegate> to your class.

then add this delegate Method:

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {

}
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
  • I know how to use tabgusters brother,i Have one idea but i dontknow how implement it can you help me out,now I have the recored video full output and i tested it the video is playing,now I have the path,now i will pass this path to the time line,now in the time line i have video url how to play the url using - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath, – Satheeshkumar Naidu Aug 09 '16 at 09:31
  • You need a full screen player or small? – gurmandeep Aug 09 '16 at 09:36
  • small i.e video must same size pf the cell when i scroll the video have to scroll with the collection view cell – Satheeshkumar Naidu Aug 09 '16 at 09:39
  • Then add a MPMoviePlayerViewController on the didselect part at that place/frame where you have the imageView – gurmandeep Aug 09 '16 at 09:41
  • we can do that bro but the requirement is like i the facebook the video will play know like that.same i have to do – Satheeshkumar Naidu Aug 09 '16 at 09:48
  • Add MPMoviePlayerViewController in every cell and then play when you get the tap – gurmandeep Aug 09 '16 at 10:06
  • how to do that,we should not tab it have to play automatically – Satheeshkumar Naidu Aug 09 '16 at 10:23
  • This should help http://stackoverflow.com/questions/19066713/how-do-i-play-video-autoplay-in-uitableviewcell-in-ios – gurmandeep Aug 09 '16 at 10:55
0

MPMoviePlayerViewController is deprecated after iOS 9. I think the better approach be to go with AVFoundation. Here is the code snippet for you.

    NSURL *vedioURL = [NSURL URLWithString:
                       @"path for your video file"];
    AVPlayer *player = [AVPlayer playerWithURL:vedioURL];
    // create a player view controller
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
    controller.player = player;
    controller.view.frame = CGRectMake(10, 40, 320 , 300);
    [player play];
    // show the view controller
    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

Note:- It won't play youtube videos, as it only plays movie files. To play other files you can use webview or may use some youtube parser to play youtube files with AVFoundation.

Sabby
  • 2,586
  • 2
  • 27
  • 41
  • Yes, You need to use AVFoundation Framework and AVKit – Sabby Aug 09 '16 at 10:50
  • can you please go through this question and if find any solution let me know – Satheeshkumar Naidu Aug 09 '16 at 10:53
  • I have posted the solution for you using AVPlayerViewController. It will show in the frame which you will specify. And if you still want to use MPMoviePlayerViewController, then this is for full screen. Just use MPMoviePlayerController – Sabby Aug 09 '16 at 10:58
  • http://stackoverflow.com/questions/38848142/paly-video-in-the-collection-view-cell-like-the-videos-playing-in-the-facebook-a please go through question the link and let me know – Satheeshkumar Naidu Aug 09 '16 at 11:02
  • I want play it the collection view cell which is having some images – Satheeshkumar Naidu Aug 09 '16 at 12:05