XCode - 让视频在像Vine& Instagram的

时间:2015-02-24 13:40:59

标签: ios xcode mpmovieplayer tableviewcell

我正在尝试在tableview中使用藤蔓类型的视频源。我希望视频在tableview单元格中一次播放一个,就像Vine和Instagram一样。

有谁知道怎么做?我正在使用MPMoviePlayer。

由于

----更新----

当每个表格单元格位于中心时,我的自动播放操作系统运行良好(请参阅“播放此内容”),但我无法让多个MPMoviePlayers加载/停止/播放

我一次只需要在其单元格中播放一个视频,但我似乎只能在第一个单元格中播放该视频并且它没有响应停止呼叫(请参阅“不要播放此视频” ')。

下面是我的脚本片段。唯一不起作用的部分是让视频实际播放和停止。播放和停止的调用工作正常。

我在我的CellForRow中有这个:

MPMoviePlayerViewController *videoPlayer;

[videoPlayer.view removeFromSuperview];

videoPlayer = [[MPMoviePlayerViewController alloc] init];


int videoTag = 500+indexPath.row;

[videoPlayer.view setTag:videoTag];

videoPlayer.moviePlayer.repeatMode = MPMovieRepeatModeOne;

videoPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;

videoPlayer.view.frame = CGRectMake( 0, 0, 320, 320);

[videoPlayer.view setBackgroundColor:[UIColor whiteColor]];

[playerView addSubview:videoPlayer.view];

我这样做是为了在视图中心调用正确的单元格:

- (void)scrollViewDidScroll:(UIScrollView *)theView{

UITableViewCell *cell = (UITableViewCell *) theView;


CGPoint currentOffset = theView.contentOffset;


for (UITableViewCell *cell in [videoTableView visibleCells]) {

    NSIndexPath *indexPath = [videoTableView indexPathForCell:cell];

    int videoTag = 500+indexPath.row;

    MPMoviePlayerViewController *videoPlayer = (MPMoviePlayerViewController *)[cell viewWithTag:videoTag];

    UILabel *test_label = (UILabel *)[cell viewWithTag:300];

    CGRect cellRect = [videoTableView convertRect:cell.bounds fromView:cell];



    int scrollPosition = currentOffset.y + 64; // 64px is the navigation bar height

    int cellPosition = cellRect.origin.y;



    if( scrollPosition > cellPosition-100 && scrollPosition < cellPosition+100 )
    {
       test_label.text = [NSString stringWithFormat:@"Play this one" ];



        UIView *blankOut = (UIView *)[cell viewWithTag:200];

        UIView *playerView = (UIView *)[cell viewWithTag:201];

        UIImageView *videoCoverUp = (UIImageView *)[cell viewWithTag:202];


        [playerView.layer setCornerRadius:138];
        [playerView.layer setMasksToBounds:YES];


        if(_isPlaying!=true){

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *path = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];

            NSData *video_data = _tableObjects[indexPath.row][9];// Where my video data is stored

            [video_data writeToFile:path atomically:YES];

            NSURL *moveUrl = [NSURL fileURLWithPath:path];


            videoPlayer.moviePlayer.contentURL = moveUrl;

            [videoPlayer.moviePlayer prepareToPlay];

            [videoPlayer.moviePlayer play];


            _isPlaying = true;

            // Needed
            [[UIApplication sharedApplication] setStatusBarHidden:NO];
            [self setNeedsStatusBarAppearanceUpdate];

        }




    } else {

        test_label.text = [NSString stringWithFormat:@"Don't play this one" ];

        [videoPlayer.moviePlayer stop];
        [videoPlayer.moviePlayer setContentURL:[NSURL URLWithString:nil]];
        [videoPlayer.moviePlayer prepareToPlay];

        //[_videoPlayer.moviePlayer.view removeFromSuperview];

        _isPlaying = false;

    }


}
}

1 个答案:

答案 0 :(得分:0)

MPMoviePlayer手册似乎描述了该怎么做?

https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html

它会向您显示在顶部做什么。看起来你只需要一个带有视图的单元格就可以将电影播放器​​添加到。

MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

根据评论,谷歌是你的朋友。必须有很多例子。

相关问题