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

时间:2016-08-16 09:25:53

标签: ios objective-c video mpmovieplayercontroller

我想在视图中播放视频(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];

但是视频不是在同一个位置播放而且正在播放-30位置。请帮助我。

img是图像视图,视频视图是视图,两者位于同一位置。

2 个答案:

答案 0 :(得分:0)

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

请查看AVPlayer

答案 1 :(得分:0)

在iOS 9中,不推荐使用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];
    });
    });
}