多个MPMoviePlayerController实例 [英] Multiple MPMoviePlayerController instances

查看:106
本文介绍了多个MPMoviePlayerController实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to put two MPMoviePlayerController on a UIView like this

    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的文档说

But only one view at a time is shown. I know Apple's documentation says


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

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.

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

but shouldn't the view show all two players at a time? Also only one instance sends MPMoviePlayerLoadStateDidChangeNotification notifications...

推荐答案

您可以使用AVFoundation框架在屏幕上播放多个视频:< a href =http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188\"rel =nofollow noreferrer> AV基础编程指南

You can play multiple videos on the screen by using the AVFoundation framework: AV Foundation Programming Guide

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

The downside is, that it is not as easy to use as the MPMoviePlayerController, and you shoul create a UIView subclass which contains the AVFoundation classes. This way you can create the necessary controls for it, control the video playback, animate the view (for example animate the transition to fullscreen).

这是我使用的方式它:

// 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的大小,即使视频播放也是如此。

You can resize the AVPlayerLayer as you like, even while the video is playing.

如果你想在调整AVPlayerLayer的大小或重新定位时使用动画,你必须改变它的默认动画,否则你会看到播放器层的视频没有与其rect同步调整大小。 (感谢@djromero的有关AVPlayerLayer动画的答案

If you want to use animations while resizing or repositioning your AVPlayerLayer, you have to alter it's default animations, otherwise you'll see that the player layer's video is not resized in sync with its rect. (Thanks to @djromero for his answer regarding the AVPlayerLayer animation)

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

Here is a small example for how to alter its default animation. The setup for this example is that the AVPlayerLayer is in a UIView subclass that acts as its container:

// 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];

}];

这篇关于多个MPMoviePlayerController实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆