MPMoviePlayerController多个视频播放

时间:2015-02-12 12:57:39

标签: ios mpmovieplayercontroller

我正在使用MPMoviePlayerController播放视频。现在我想循环播放多个视频。这是我的代码:

self.movieController = [[MPMoviePlayerController alloc] init];

    NSURL *url = [NSURL URLWithString:@"My Video Path"];
    [self.movieController setContentURL:url];
    [self.movieController.view setFrame:CGRectMake (0.0f, 0.0f, 320.0f, 370.0f)];
     self.movieController.controlStyle = MPMovieControlStyleDefault;
    [self.movieController setFullscreen:YES animated:YES];
   // self.movieController.movieSourceType = MPMovieSourceTypeFile;
    [self.view addSubview:self.movieController.view];

    [self.movieController play];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.movieController];

- (void)moviePlayBackDidFinish:(NSNotification *)notification {

[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

[self.movieController stop];
[self.movieController.view removeFromSuperview];
self.movieController = nil;

}

1个视频正常工作。现在,假设我有两个URL将一个接一个地自动播放。我该怎么办?

2 个答案:

答案 0 :(得分:1)

MPMoviePlayerController上每次要更改视频时,您需要setContentURL。我建议moviePlayBackDidFinish不要杀死播放器但是添加另一个播放器 [self.movieController setContentURL:url];play(或prepareToPlay

这不会立即发生,所以也许你需要一个UIActivityIndicatorView

修改

选项编号2,如果您想直接使用AVPlayer - 您有AVQueuePlayer

答案 1 :(得分:1)

- (void)moviePlayBackDidFinish:(NSNotification *)notification {

    if (needNextPlay) {
        MPMoviePlayerController *player = (MPMoviePlayerController *)[notification object];
        NSURL *url = [NSURL URLWithString:@"Your new Video Path"];
        [player setContentURL:url];
    } else {
        [self.movieController stop];
        [self.movieController.view removeFromSuperview];
        self.movieController = nil;
    }

}