Satheeshkumar Naidu Satheeshkumar Naidu - 3 months ago 30
Objective-C Question

How to play video using Mpmovie player in the exactly in the view

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.

Answer

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