2010-12-06 57 views
23

我試圖將兩個的MPMoviePlayerController上一個UIView這樣多的MPMoviePlayerController實例

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

但只有一次一個視圖。我知道,蘋果的文件說

注:雖然您可以創建多個的MPMoviePlayerController對象,並在你的界面提出自己的看法,只有一次一個電影播放器​​可以發揮其電影。

但不應該一次顯示所有兩個玩家的視圖嗎?也只有一個實例發送MPMoviePlayerLoadStateDidChangeNotification通知...

+0

找到的答案? – Pompair 2011-03-24 18:06:30

+0

沒有。被迫使用嵌入式視頻的UIWebView。 – 2011-03-28 15:11:57

回答

0

您是否需要同時播放這兩個視頻?

我想我有兩個選擇:

  1. 添加在同一屏幕上的兩個視頻縮略圖按鈕。根據選擇選擇的視頻播放和其他得到停止。所以在某一時刻,只有一個視頻纔會播放。

  2. 創建一個單獨的視圖控制器並將視頻添加到該視圖中。現在,無論你想要在你的主視圖中添加viewcontroller.view。

讓我知道這些工作是否適合你。

1

您將無法做到這一點。內部MPMoviePlayerController似乎使用相同的視圖來交換實例,具體取決於正在播放的任何實例。您唯一的選擇是使用AVFoundation開發自己的視頻播放器以獲取多個實例,或使用HTML和UIWebViews。請參閱:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188

+0

這只是簡單的破碎:如果你在同一個屏幕上顯示兩個MPMoviePlayerViewController,簡單地說,它們都是黑色的,沒有任何作用! (2013年12月) – Fattie 2013-12-01 20:51:19

27

您可以在屏幕上播放多個視頻使用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]; 

}];