Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以在UICollectionView中查看MPMoviePlayerController?_Ios_Video_Collections_Mpmovieplayercontroller_Uicollectionview - Fatal编程技术网

Ios 是否可以在UICollectionView中查看MPMoviePlayerController?

Ios 是否可以在UICollectionView中查看MPMoviePlayerController?,ios,video,collections,mpmovieplayercontroller,uicollectionview,Ios,Video,Collections,Mpmovieplayercontroller,Uicollectionview,我正在尝试构建一个UICollectionView,其中每个单元格都包含一个带有不同视频的MPMoviePlayerController。我希望用户能够在适当的位置播放每个视频 我知道不能播放MPMoviePlayerController的多个实例。苹果的文档模糊不清的是,你是否可以实例化它们并将它们放在视图中——按照我的阅读方式,你实际上可以做到这一点。然而,我无法让它发挥作用,所以我最终放弃了,尝试了一种不同的技术 相反,我尝试使用select事件在采集单元中实例化、放置并自动播放视频。作为

我正在尝试构建一个UICollectionView,其中每个单元格都包含一个带有不同视频的MPMoviePlayerController。我希望用户能够在适当的位置播放每个视频

我知道不能播放MPMoviePlayerController的多个实例。苹果的文档模糊不清的是,你是否可以实例化它们并将它们放在视图中——按照我的阅读方式,你实际上可以做到这一点。然而,我无法让它发挥作用,所以我最终放弃了,尝试了一种不同的技术

相反,我尝试使用select事件在采集单元中实例化、放置并自动播放视频。作为控件,我在集合视图之外的同一UIViewController中放置了一个附加视图。当我运行它时,外部视图加载得很好——视频被实例化,第一帧出现了。集合视图中的所有单元格也显示正确。但是,当我选择其中一个采集单元时,外部视图中的视频完全消失,而采集视图中的视频已实例化,但未能显示。我知道它是实例化的,因为我对每个视图的背景进行了颜色编码,在选择单元格时,视频的背景出现了,但实际的视频没有。非常混乱

所以我想知道你是否可以把一个视频放在一个集合视图中

以下是视图控制器的代码:

#import "testViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "tvCollectionCell.h"

@interface testViewController ()

@property (strong) IBOutlet UIView *containerView;
@property (strong) MPMoviePlayerController *videoPlayer;

@end

@implementation testViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    self.videoPlayer = [[MPMoviePlayerController alloc]
                        initWithContentURL: [NSURL fileURLWithPath:
                                             [[NSBundle mainBundle]
                                              pathForResource:@"testvideo" ofType:@"mp4"]]];

    CGRect videoFrame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);

    [[self.videoPlayer view] setFrame:videoFrame];

    self.videoPlayer.shouldAutoplay = FALSE;
    self.videoPlayer.repeatMode = TRUE;
    [self.videoPlayer prepareToPlay];
    [self.videoPlayer view].backgroundColor = [UIColor orangeColor];

    [self.containerView addSubview:[self.videoPlayer view]];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

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

    tvCollectionCell *tvCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tvCell" forIndexPath:indexPath];
    tvCell.backgroundColor = [UIColor purpleColor];

    return tvCell;

}

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

    tvCollectionCell *tvCell = (tvCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];

    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
                                      initWithContentURL: [NSURL fileURLWithPath:
                                                           [[NSBundle mainBundle]
                                                            pathForResource:@"testvideo" ofType:@"mp4"]]];

    CGRect videoFrame = CGRectMake(0, 0, tvCell.frame.size.width, tvCell.frame.size.height);

    [[movie view] setFrame:videoFrame];

    movie.shouldAutoplay = TRUE;
    movie.repeatMode = TRUE;
    [movie prepareToPlay];
    [movie view].backgroundColor = [UIColor blueColor];

    [tvCell addSubview:[movie view]];

}

@end

谢谢你的帮助

您是否确保所有视频都有引用? 这可能是MPMoviePlayerController又名videoPlayer的唯一属性
如果正在重写,您可以尝试分配一个数组来保存对每个视频的引用,然后检索该引用并播放该视频控制器。

self.videoPlayer是与单个单元格中的视频分开实例化的控制视频。在didSelectItemAtIndexPath中,您将看到每次选择单元格时新的MPMoviePlayerController被实例化的位置。对此有任何更新吗?你明白了吗?