在集合视图单元格中播放视频,就像在 Facebook 应用程序时间轴中播放的视频一样

2024-05-13

我想在集合视图单元格中播放视频,要求就像 Instagram 时间线,播放视频就像在 facebook 时间线中播放视频,

为此,我使用了 UICollectionViewCell,我有一些图像,没有视频,现在我是来自画廊的图像,我正在使用相机拍摄图像,并且正在录制视频,每次我都会从上面取出任何一个,然后输出我已添加到时间线。

例如,我们采用 3vc,第一个 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) receiveArray:(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) receivetitleArray:(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 的新手,请帮助我,感谢您的快速回复


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

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

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在集合视图单元格中播放视频,就像在 Facebook 应用程序时间轴中播放的视频一样 的相关文章

随机推荐