ios4 - 多个MPMoviePlayerController实例

标签 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];
}

但是一次只显示一个 View 。我知道苹果的文件说

Note: Although you may create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time may play its movie.



但是 View 不应该一次显示所有两个玩家吗?同样只有一个实例发送MPMoviePlayerLoadStateDidChangeNotification通知...

最佳答案

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

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

这是我的用法:

// 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的大小或位置时使用动画,则必须更改其默认动画,否则您将看到播放器层的视频未与其矩形同步调整大小。 (感谢@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];

}];

关于ios4 - 多个MPMoviePlayerController实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4368112/

相关文章:

objective-c - iPhone应用程序编程-导航到其他 View Controller 和转义导航 Controller

iphone - 在拖动时如何将 UISlider THUMB 直接移动/跳转到下一个值

iphone - 日志调试 Objective-C 代码时使用哪些日志记录解决方案?

ios - 使用 MPMoviePlayerController 恢复中断的广播流

iOS 将 MPMoviePlayerController 镜像到电视,全屏播放电视屏幕

iphone - 如何使用 iphone sdk 从 SQLite 检索数据?

iphone - UIImagePickerControllerDelegate 没有正确响应

android - ios, android, windows 移动平台上的蓝牙 sdk

ios - Sharepoint 2010,HTML5 视频无法在 iPad 上运行

ios - MPMoviePlayerController 的初始和结束时间属性...具有破坏性?