0

I want play an video in the view(6,60,412,229)

I Added below code.

cell.movieplayer = [[MPMoviePlayerController alloc]initWithContentURL:app.array1[row]];
cell.videoview.hidden=NO;
cell.img.hidden=YES;
// [[cell.movieplayer view]setFrame:CGRectMake(0.0f, 0.0f, 412.0f, 221.0f)];
cell.movieplayer.view.frame = cell.img.frame;
[cell.videoview addSubview:cell.movieplayer.view];
NSLog(@"%@",cell.movieplayer);
[cell.movieplayer play];

But video is not playing at the same position and it is playing -30 position.please help me out.

img is the image view and video view is view and both are at the same position.

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57

2 Answers2

0

You probably don't want to use MPMoviePlayerController since it's old and deprecated in iOS 9.

Check out AVPlayer instead.

David
  • 125
  • 2
  • 15
0

In iOS 9, MPMoviePlayerViewController will be deprecated. It's probably about time to move away from it, in favour of the AVKit classes, like AVKitPlayerViewController. This could be cause for some refactoring, since AVKitPlayerViewController cannot be subclassed, but if this keeps XCDYouTubeKit working in the future (and with features like Picture-in-Picture), then it should be worth the effort in the long run.

// grab a local URL to our video
NSURL *videoURL = [NSURL URLWithString:@"Your_Url"];

// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];

// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];

// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;

if you want to play the video in cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{
    cell.videoItem = [AVPlayerItem playerItemWithURL:url];

    dispatch_sync(dispatch_get_main_queue(), ^{
        cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
        cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
        cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
        [cell.videoItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
        [cell.videoItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStalledNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

         cell.avLayer.frame = CGRectMake(5, 9, 310, 310);
         [cell.contentView.layer addSublayer:  cell.avLayer];
         [ cell.videoPlayer play];
         [cell.contentView addSubview:cell.videoActivity];
    });
    });
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Amit Srivastava
  • 1,105
  • 9
  • 18
  • Thanks for your valuable answer.I want declare the avpalyer in the cell like @property() avplayer *player; later in the indext path cell.aavplayeronj like this becz the video must play in the cell. – Satheeshkumar Naidu Aug 16 '16 at 09:56
  • cell.videoPlayer,cell.videoPlayer where and how did declare this – Satheeshkumar Naidu Aug 16 '16 at 10:16
  • you have to declared this `AVPlayer * videoPlayer = [AVPlayer playerWithURL:videoURL];` instead of`AVPlayer *player = [AVPlayer playerWithURL:videoURL];` – Amit Srivastava Aug 16 '16 at 10:22
  • http://stackoverflow.com/questions/38848142/paly-video-in-the-collection-view-cell-like-the-videos-playing-in-the-facebook-a this my requirement and can you pls sugest modifications just use avplayer in places of mpmovieplayer thats it, – Satheeshkumar Naidu Aug 16 '16 at 10:25