如何在视图中使用 Mpmovie 播放器播放视频

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

我想在视图中播放视频(6,60,412,229)

我添加了下面的代码。

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

但是视频没有在同一个位置播放,而是在播放-30position.please帮帮我。

img是image view,video view是view,两者位置相同

您可能不想使用 MPMoviePlayerController,因为它已经过时并且在 iOS 9.

中已弃用。

改为查看 AVPlayer

在iOS9中,MPMoviePlayerViewController将被弃用。可能是时候放弃它,支持 AVKit 类,比如 AVKitPlayerViewController。这可能是一些重构的原因,因为 AVKitPlayerViewController 不能被子类化,但如果这让 XCDYouTubeKit 在未来继续工作(以及像画中画这样的功能),那么在长期 运行 中应该值得付出努力.

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

如果要在单元格中播放视频:

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