多个MPMoviePlayerController实例

时间:2010-12-06 15:46:43

标签: ios4 ipad mpmovieplayercontroller

我正在尝试将两个MPMoviePlayerController放在像这样的UIView上

    for (int i = 0; i < 2; i++)
{
    UIView* v = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 300.0f * i, self.view.width, 300.0f)];
    [self.view addSubview: v];

    NSURL* url = [NSURL URLWithString: [urls objectAtIndex: i]];
    MPMoviePlayerController* movieController = [[MPMoviePlayerController alloc] initWithContentURL: url];
    movieController.movieSourceType = MPMovieSourceTypeFile;
    movieController.shouldAutoplay = NO;
    movieController.view.frame = v.bounds;
    [self.view addSubview: movieController.view];
}

但是一次只显示一个视图。我知道Apple的文档说

  

注意:虽然您可以创建多个MPMoviePlayerController对象并在界面中显示其视图,但一次只能有一个电影播放器​​播放其电影。

但是视图不应该同时显示所有两个玩家吗?此外,只有一个实例发送MPMoviePlayerLoadStateDidChangeNotification通知...

3 个答案:

答案 0 :(得分:27)

您可以使用AVFoundation框架在屏幕上播放多个视频:AV Foundation Programming Guide

缺点是,它不像MPMoviePlayerController那样容易使用,你应该创建一个包含AVFoundation类的UIView子类。这样,您可以为其创建必要的控件,控制视频播放,为视图设置动画(例如,将过渡设置为全屏幕的动画)。

这就是我使用它的方式:

// Create an AVURLAsset with an NSURL containing the path to the video
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

// Create an AVPlayerItem using the asset
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];

// Create the AVPlayer using the playeritem
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

// Create an AVPlayerLayer using the player
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

// Add it to your view's sublayers
[self.layer addSublayer:playerLayer];

// You can play/pause using the AVPlayer object
[player play];
[player pause];

// You can seek to a specified time
[player seekToTime:kCMTimeZero];

// It is also useful to use the AVPlayerItem's notifications and Key-Value 
// Observing on the AVPlayer's status and the AVPlayerLayer's readForDisplay property
// (to know when the video is ready to be played, if for example you want to cover the 
// black rectangle with an image until the video is ready to be played)
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification 
                                           object:[player currentItem]];

    [player addObserver:self forKeyPath:@"currentItem.status"
                     options:0
                     context:nil];

    [playerLayer addObserver:self forKeyPath:@"readyForDisplay"
                     options:0
                     context:nil];

您可以根据需要调整AVPlayerLayer的大小,即使在播放视频时也是如此。

如果要在调整AVPlayerLayer大小或重新定位时使用动画,则必须更改其默认动画,否则您将看到播放器图层的视频未与其rect同步调整大小。 (感谢@djromero为他的answer regarding the AVPlayerLayer animation

这是一个如何改变其默认动画的小例子。此示例的设置是AVPlayerLayer位于充当其容器的UIView子类中:

// UIView animation to animate the view
[UIView animateWithDuration:0.5 animations:^(){
    // CATransaction to alter the AVPlayerLayer's animation
    [CATransaction begin];
    // Set the CATransaction's duration to the value used for the UIView animation
    [CATransaction setValue:[NSNumber numberWithFloat:0.5] 
                     forKey:kCATransactionAnimationDuration];
    // Set the CATransaction's timing function to linear (which corresponds to the 
    // default animation curve for the UIView: UIViewAnimationCurveLinear)
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction 
                             functionWithName:kCAMediaTimingFunctionLinear]];

        self.frame = CGRectMake(50.0, 50.0, 200.0, 100.0);
        playerLayer.frame = CGRectMake(0.0, 0.0, 200.0, 100.0);

    [CATransaction commit];

}];

答案 1 :(得分:1)

你将无法做到这一点。内部MPMoviePlayerController似乎使用相同的视图在实例之间进行交易,具体取决于正在播放的实例。您唯一的选择是使用AVFoundation开发自己的视频播放器以获取多个实例,或使用HTML和UIWebViews。请参阅:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188

答案 2 :(得分:0)

您是否需要同时播放这两个视频?

我想我有两个选择:

  1. 在同一屏幕上添加两个视频缩略图图像按钮。根据选择选择的视频播放和其他停止。因此,在视图中,只会播放单个视频。

  2. 创建一个单独的视图控制器并将视频添加到其中。现在将viewcontroller.view添加到主视图中的任何位置。

  3. 如果其中任何一项适合您,请告诉我。