Play video in the collection view cell like the vi

2019-02-26 06:01发布

问题:

I want to play video in the collection view cell,the requirement is like the instagram timeline and playing video is like playing videos in the facebook time line,

for this I used UICollectionViewCell in witch I have some images no videos now I am images from gallery,I am taking the image using camera and I am recording the video,every time I will have any one out from the above and the out put i have add to the time line.

For example, we take 3vc 1st vc is having collection view with some images,I the second vc we are getting output either it is video,images,I am taking the images and the first frame of the image in to the common array which is in the VC3 from VC3 I am passing the array and the output video path url to the 1stVC using notification center

 - (IBAction)sharebuttn:(id)sender {

[self dismissViewControllerAnimated:YES completion:^{

    //  Tabbar index

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ShareArray" object:_selectedimgarray];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SharetitleArray" object:_newtile];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"sharevideooutputurl" object:_finalvideourl];

}];

and In the 1stVC I am retrieving them like this

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedArray:) name:@"ShareArray" object:nil];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedtitleArray:) name:@"SharetitleArray" object:nil];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sharevideooutputurl:) name:@"Sharevideourl" object:nil];

-(void) receivedArray:(NSNotification*)notification {

    NSMutableArray* userInfo = notification.object;
UIImage *image = [userInfo firstObject];


if ([userInfo count]>0) {

     //[_imagearray insertObject:[userInfo firstObject] atIndex:0];
    [app.array1 insertObject:[userInfo firstObject] atIndex:0];
    [self.collection reloadData];
    NSLog(@"%@",app.array1);

}
 //[_imagearray insertObject:[userInfo firstObject] atIndex:0];

 // [self.collection reloadData];
_collection.delegate=self;
_collection.dataSource=self;

}

-(void) receivedtitleArray:(NSNotification*)notification {

    NSMutableArray* userInfotitle = notification.object;
NSLog(@"%@",userInfotitle);
//[_tittlearray insertObject:[userInfotitle firstObject] atIndex:0];

 [app.tarray1 insertObject:[userInfotitle firstObject] atIndex:0];
 NSLog(@"%@",app.tarray1);
//NSLog(@"%@",_tittlearray);



_collection.delegate=self;
_collection.dataSource=self;    

[self.tabBarController setSelectedIndex:0];


     //[self.collection reloadData];

} -(void) sharevideooutputurl:(NSNotification*)notification {

NSURL *finalsharevideourl=notification.object;

[self.collection reloadData];
_collection.delegate=self;
_collection.dataSource=self;

}

and In the collection viewcell

  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

 //[self.collection reloadData];
homeceeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];


UIImage *image;
NSInteger row = [indexPath row];


NSLog(@"indexpath = %ld", (long)row);
 if( [app.array1[row] isKindOfClass:[UIImage class]]) {
   image= app.array1[row];
}
 else
 {
    image = [UIImage imageNamed:app.array1[row]];

}
cell.img.image = image;

cell.text.text=[app.tarray1 objectAtIndex:indexPath.row];


    return cell;

} Image and video first frame image is adding successfully I want to video also,when i scroll If the indexpath having any video contant the video have play automatically in the cell, in this url "finalsharevideourl "I have complete path

I am new to the objective c,some please help me out,Thanks for the quick responce

回答1:

Finally I found answer for this,just change below code

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

 //[self.collection reloadData];
homeceeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIImage *image;
NSInteger row = [indexPath row];
NSURL *furl;

NSLog(@"indexpath = %ld", (long)row);

if ([app.array1[row] isKindOfClass:[NSURL class]])
{
      furl=app.array1[row];
    cell.movieplayer = [[MPMoviePlayerController alloc]initWithContentURL:app.array1[row]];

    cell.videoview.hidden=NO;
    cell.img.hidden=YES;

    cell.movieplayer.view.frame = cell.img.frame;
    [cell.videoview addSubview:cell.movieplayer.view];

    NSLog(@"%@",cell.movieplayer);
    [cell.movieplayer play];

            NSLog(@"%@",cell.movieplayer);
}
else {
    if ([app.array1[row] isKindOfClass:[UIImage class]]) {
        image= app.array1[row];
        cell.img.hidden=NO;
         cell.videoview.hidden=YES;
    }
    else {
        image = [UIImage imageNamed:app.array1[row]];
         cell.videoview.hidden=YES;
        cell.img.hidden=NO;
    }
    cell.img.image = image;
}

cell.text.text=[app.tarray1 objectAtIndex:indexPath.row];
return cell;

}