在收藏视图单元格中播放视频,例如在Facebook应用时间线

时间:2016-08-09 10:21:41

标签: ios objective-c mpmovieplayercontroller uicollectionviewcell avplayerlayer

我想在集合视图单元格中播放视频,要求就像instagram时间线,播放视频就像在Facebook时间线上播放视频一样,

为此,我使用UICollectionViewCell女巫我有一些图像没有视频现在我是来自画廊的图像,我正在使用相机拍摄图像,我正在录制视频,每次我将从上面的任何一个和out put我已添加到时间线。

例如,我们拿3vc 1st vc有一些图像的集合视图,我第二个vc我们正在输出它是视频,图像,我拍摄的图像和图像的第一帧到共同点从VC3到VC3的数组我正在使用通知中心将数组和输出视频路径url传递给1stVC

 - (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];

}];

并且在1stVC中我正在检索它们

[[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 *)通知 {

    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 *)通知 {

    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 *)通知 {

NSURL *finalsharevideourl=notification.object;

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

}

并在集合视图中

  - (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;

} 图像和视频第一帧图像正在成功添加我想要视频,当我滚动如果索引路径有任何视频包含视频已经在单元格中自动播放,在此网址“finalsharevideourl”我有完整的路径

我是目标c的新手,有些人请帮助我,谢谢您的快速反应

1 个答案:

答案 0 :(得分:1)

最后我找到了答案,只需更改下面的代码

- (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;

}