如何自定义Mpmoviewplayer大小并使其适合collectionview单元格

时间:2016-08-05 11:16:56

标签: objective-c mpmovieplayercontroller uicollectionviewcell

我正在使用以下代码

在集合视图中的文档目录中播放视频
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{

        if (indexPath.item==0) {

        NSURL *vedioURL;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

        NSLog(@"files array %@", filePathsArray);

        //reverse the array
        //    NSArray* reversedArray = [[filePathsArray reverseObjectEnumerator] allObjects];
        //    NSLog(@"%@",reversedArray);

        NSString *fullpath;

        for ( NSString *apath in filePathsArray )
        {
            fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
            vedioURL =[NSURL fileURLWithPath:fullpath];
        }
        NSLog(@"vurl %@",vedioURL);

        MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
        videoPlayerView.view.frame = CGRectMake(0, 60, 412,229);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
        [view addSubview:videoPlayerView.view];
        [self.view addSubview:view];
        [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
        [videoPlayerView.moviePlayer play];


    }

当我点击正在播放的图像时,我希望该视频在同一个单元格中的相同位置播放。

4 个答案:

答案 0 :(得分:0)

    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
    videoPlayerView.view.frame = CGRectMake(0, 60, 412,229);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    [view addSubview:videoPlayerView.view];
    [self.view addSubview:view];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];
    [view setClipsToBounds:YES];

答案 1 :(得分:0)

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

@try {


    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    // A safe check before going to access sibview.
    if ([cell.contentView.subviews count] > 0) 
    {
      // Find your imageview here
    }
}
@catch (NSException *exception) {
    NSLog(@"Exception :%@",exception.debugDescription);
}
}

答案 2 :(得分:0)

点按手势

UITapGestureRecognizer *singleFingerTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self 
                                        action:@selector(handleSingleTap:)];
singleFingerTap.delegate = self;
[yourimageview addGestureRecognizer:singleFingerTap];

请尝试以下代码:

conform the <UIGestureRecognizerDelegate> to your class.

然后添加此委托方法:

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {

}

答案 3 :(得分:0)

在iOS 9之后不推荐使用MPMoviePlayerViewController。 我认为更好的方法是使用AVFoundation。 这是您的代码段。

    NSURL *vedioURL = [NSURL URLWithString:
                       @"path for your video file"];
    AVPlayer *player = [AVPlayer playerWithURL:vedioURL];
    // create a player view controller
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
    controller.player = player;
    controller.view.frame = CGRectMake(10, 40, 320 , 300);
    [player play];
    // show the view controller
    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

注意: - 它不会播放youtube视频,因为它只播放电影文件。要播放其他文件,您可以使用webview或使用某些youtube解析器播放AVFoundation的YouTube文件。