首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个MPMoviePlayerController实例

多个MPMoviePlayerController实例
EN

Stack Overflow用户
提问于 2010-12-06 15:46:43
回答 3查看 7K关注 0票数 23

我想把两个MPMoviePlayerController放在这样的UIView上

代码语言:javascript
复制
    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];
}

但一次只显示一个视图。我知道苹果的文件上写着

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

但是这个观点不应该一次让两位球员都看到吗?而且只有一个实例发送MPMoviePlayerLoadStateDidChangeNotification通知..。

EN

回答 3

Stack Overflow用户

发布于 2012-06-13 07:55:57

您可以使用AVFoundation框架:AV基础程序设计指南在屏幕上播放多个视频。

缺点是,它不像MPMoviePlayerController那样容易使用,您应该创建一个包含AVFoundation类的UIView子类。通过这种方式,您可以为它创建必要的控件,控制视频回放,动画视图(例如,动画化到全屏的转换)。

我就是这样用它的:

代码语言:javascript
复制
// 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的关于AVPlayerLayer动画的答案)

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

代码语言:javascript
复制
// 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];

}];
票数 27
EN

Stack Overflow用户

发布于 2012-06-01 19:05:26

你不能这么做。在内部,MPMoviePlayerController似乎使用相同的视图在实例之间进行交换,这取决于所播放的实例。您唯一的选择是使用AVFoundation开发自己的视频播放器,以获得多个实例,或者使用HTML和UIWebViews。请参阅:参考文件/doc/uid/uid 40010188

票数 1
EN

Stack Overflow用户

发布于 2012-01-14 12:45:32

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

我想我有两个选择:

  1. 在同一屏幕上添加两个视频缩略图图像按钮。根据选择,选定的视频被播放,其他的被停止。因此,在某一时刻,只有单一的视频将被播放。
  2. 创建一个单独的视图控制器并将您的视频添加到其中。现在,将viewcontroller.view添加到您想要的主要视图中。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4368112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档