Ответ 1
Вы можете воспроизводить несколько видеороликов на экране с помощью рамки AVFoundation: Руководство по программированию AV Foundation
Недостатком является то, что он не так прост в использовании, как MPMoviePlayerController, и вы создаете подкласс UIView, который содержит классы AVFoundation. Таким образом, вы можете создать для него необходимые элементы управления, управлять воспроизведением видео, анимировать представление (например, оживить переход на полноэкранный режим).
Вот как я его использовал:
// 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 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 notifications and Key-Value
// Observing on the AVPlayer status and the AVPlayerLayer 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 за ответ относительно анимации AVPlayerLayer)
Вот небольшой пример того, как изменить свою анимацию по умолчанию. Настройка для этого примера заключается в том, что AVPlayerLayer находится в подклассе UIView, который действует как его контейнер:
// UIView animation to animate the view
[UIView animateWithDuration:0.5 animations:^(){
// CATransaction to alter the AVPlayerLayer animation
[CATransaction begin];
// Set the CATransaction duration to the value used for the UIView animation
[CATransaction setValue:[NSNumber numberWithFloat:0.5]
forKey:kCATransactionAnimationDuration];
// Set the CATransaction 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];
}];