您可以在屏幕上播放多個視頻使用AVFoundation框架:AV Foundation Programming Guide
的缺點是,它是不容易的的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'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大小或重新定位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];
}];
找到的答案? – Pompair 2011-03-24 18:06:30
沒有。被迫使用嵌入式視頻的UIWebView。 – 2011-03-28 15:11:57