多个MPMoviePlayerController实例

Kos*_*sky 23 mpmovieplayercontroller ipad ios4

我试图在这样的UIView上放两个MPMoviePlayerController

    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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Alp*_*pár 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];
Run Code Online (Sandbox Code Playgroud)

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

如果要在调整AVPlayerLayer大小或重新定位时使用动画,则必须更改其默认动画,否则您将看到播放器图层的视频未与其rect同步调整大小.(感谢@djromero 关于AVPlayerLayer动画的答案)

这是一个如何更改其默认动画的小例子.此示例的设置是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];

}];
Run Code Online (Sandbox Code Playgroud)


Mru*_*nal 0

您需要同时播放两个视频吗?

我想我有两个选择:

  1. 在同一屏幕上添加两个视频缩略图按钮。根据选择,播放所选视频并停止其他视频。因此,一次观看时只会播放单个视频。

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

让我知道这些是否对您有用。