Ios4 多个MPMoviePlayerController实例
我试着把两个MPMoviePlayerController放在这样一个UIView上Ios4 多个MPMoviePlayerController实例,ios4,ipad,mpmovieplayercontroller,Ios4,Ipad,Mpmovieplayercontroller,我试着把两个MPMoviePlayerController放在这样一个UIView上 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 URLWithStri
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];
}
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:[url对象索引:i]];
MPMoviePlayerController*movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.movieSourceType=mpmoviesourcetype文件;
movieController.shouldAutoplay=否;
movieController.view.frame=v.bounds;
[self.view addSubview:movieController.view];
}
但一次只显示一个视图。我知道苹果的文件上说
注意:尽管您可以创建多个MPMoviePlayerController对象并在界面中显示它们的视图,但一次只能有一个电影播放器播放其电影
但是这个视图不应该同时显示所有两个玩家吗?此外,只有一个实例发送MPMoviePlayerLoadStateDidChangeNotification通知…是否需要同时播放这两个视频 我想我有两个选择:
如果其中任何一项对您有效,请告诉我。您将无法执行此操作。在内部,MPMoviePlayerController似乎使用相同的视图在实例之间进行交易,具体取决于正在播放的实例。您唯一的选择是使用AVFoundation开发自己的视频播放器,以获得多个实例,或者使用HTML和UIWebView。请参阅:您可以使用AVFoundation framework在屏幕上播放多个视频: 缺点是,它不像MPMoviePlayerController那么容易使用,您应该创建一个包含AVFoundation类的UIView子类。通过这种方式,您可以为其创建必要的控件、控制视频播放、设置视图动画(例如设置转换到全屏的动画) 下面是我如何使用它的:
// 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的大小或重新定位时使用动画,则必须更改其默认动画,否则您将看到播放器层的视频大小不会与其矩形同步。(感谢@djromero的帮助)
下面是一个如何更改其默认动画的小示例。本例的设置是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];
}];
不。被迫使用带有嵌入式视频的UIWebView。这完全是个错误:如果在同一屏幕上显示两个MPMoviePlayerServiceWController,很简单,它们都变黑了,没有任何效果!(2013年12月)实际上,最后添加的一个播放视频,但其他人只显示背景色。@Alpar-这很有魅力,但有一个问题。为什么在添加observer AVPlayerItemDidPlayToEndTimeNotification时,对象被指定为[player currentItem]?如果我只是说玩家呢?如果我指定player,那么将为所有视频实例发送通知。