Sie können mithilfe des AVFoundation Rahmen mehrere Videos auf dem Bildschirm spielen: AV Foundation Programming Guide
Der Nachteil ist, dass es nicht so einfach zu bedienen wie die MPMoviePlayerController ist, und Sie shoul eine UIView Unterklasse schaffen, enthält die AVFoundation-Klassen. Auf diese Weise können Sie die erforderlichen Steuerelemente dafür erstellen, die Videowiedergabe steuern und die Ansicht animieren (z. B. den Übergang zum Vollbild animieren).
Hier ist es, wie ich es verwendet:
// 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];
Sie können die AVPlayerLayer Größe ändern, wie Sie möchten, auch während das Video abgespielt wird.
Wenn Sie Animationen bei der Größenanpassung oder Neupositionierung Ihres AVPlayerLayers verwenden möchten, müssen Sie die Standardanimationen ändern. Andernfalls sehen Sie, dass das Video des Player-Layers nicht synchron mit seinem Rect skaliert wird. (Danke an @djromero für seine answer regarding the AVPlayerLayer animation)
Hier ist ein kleines Beispiel, wie man seine Standardanimation ändert. In diesem Beispiel befindet sich der AVPlayerLayer in einer UIView-Unterklasse, die als sein Container fungiert:
// 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];
}];
Haben Sie eine Antwort darauf gefunden? – Pompair
Nein. Wurde gezwungen, ein UIWebView mit eingebetteten Videos zu verwenden. –