简体   繁体   中英

XCode - Getting videos to play in tableview cells like Vine & Instagram

I'm trying to get a vine type video feed working in a tableview. I want the videos to play one at a time inside the tableview cell just like Vine and Instagram.

Does anyone know how to do this? I'm using MPMoviePlayer.

Thanks

---- UPDATE ----

I've got the auto-play call-to-action working well (please see 'Play this one') when each table cell is in the center but I'm having trouble getting multiple MPMoviePlayers to load/stop/play.

I only require one video to play in its cell at any one time but I can only seem to get the video in the first cell playing and it is not responding to the Stop call (please see 'Don't play this one' ).

Below is a snippet of my script. The only part not working is getting the videos to actually play and stop. The call to play and stop works fine.

I have this in my 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];

And I do this for calling the correct cell in the center of the 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;

    }


}
}

The MPMoviePlayer manual seems to describe what to do?

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

It shows you what to do right at the top. Seems you will just need a cell with a view in it for you to add the movie player to.

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

As per the comment, google is your friend. Must be lots of examples out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM