【问题标题】:Play video in the collection view cell like the videos playing in the Facebook app Timeline在集合视图单元格中播放视频,就像在 Facebook 应用程序时间轴中播放的视频一样
【发布时间】:2017-03-19 20:20:56
【问题描述】:

我想在收藏视图单元格中播放视频,要求就像 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) 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];

} -(无效)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;

} 图像和视频第一帧图像添加成功我也想视频,当我滚动时如果索引路径有任何视频内容视频在单元格中自动播放,在这个 url “finalsharevideourl”我有完整的路径

我是目标c的新手,请帮助我,感谢您的快速响应

【问题讨论】:

  • @Satheeshkumar 你能分享我的示例项目吗

标签: ios objective-c mpmovieplayercontroller uicollectionviewcell avplayerlayer


【解决方案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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2014-05-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多