播放两个视频接连导致黑屏短

时间:2012-03-08 10:17:01

标签: iphone objective-c mpmovieplayercontroller

我有一个应用程序,我在触摸视图后播放电影,然后在电影结束时播放。我在视图上放置了一个按钮。单击按钮时,将播放第二部电影。但它似乎只是加载后出现一个短黑屏。我现在想避开黑屏...

下面是代码:

viewDidLoad

中的初始化
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSString *movpath = [[NSBundle mainBundle] 
                     pathForResource:@"movie1" 
                     ofType:@"m4v"];

mpviewController = 
[[MPMoviePlayerViewController alloc]
 initWithContentURL:[NSURL fileURLWithPath:movpath]]; 

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(movieFinishedCallback:)                                                 
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:nil];


mp = [mpviewController moviePlayer];
// [mp setMovieControlMode:MPMovieControlModeHidden];
// [mp.view setFrame:CGRectMake(0, 0, 250, 263)]; 
mp.controlStyle = MPMovieControlStyleNone;    
mp.shouldAutoplay = NO;

[mp prepareToPlay];
[mp pause];

然后在故事板上触摸我呼叫startAnimation

- (IBAction)startAnimation:(id)sender {

     NSLog(@"Animation 1");  
    [self.view addSubview:mpviewController.view];
    [mp play];
}

这部电影结束后我设置了按钮

- (void) movieFinishedCallback:(NSNotification*) aNotification {
   NSLog(@"...movie done");

// generate start button for animation 2
startAnimation2Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
startAnimation2Button.tag = 1;
[startAnimation2Button addTarget:self action:@selector(startAnimation2:) forControlEvents:UIControlEventTouchUpInside];
startAnimation2Button.frame = CGRectMake(130, 230, 070, 070);
startAnimation2Button.userInteractionEnabled = YES;
startAnimation2Button.alpha = 0.1;

[self.view addSubview:startAnimation2Button];
}

然后触摸按钮后,第二个动画开始

- (IBAction)startAnimation2:(id)sender  {

    NSLog(@"Animation 2");
    NSString *movpath = [[NSBundle mainBundle] 
                     pathForResource:@"movie2" 
                     ofType:@"m4v"];
    [mp setContentURL:[NSURL fileURLWithPath:movpath]];

    [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(movieFinishedCallback2:)                                                 
         name:MPMoviePlayerPlaybackDidFinishNotification
         object:nil];

    [mp play];
}

但是这里出现了一个黑色的短屏幕,可能是在加载了movie2然后正在播放时。

如何避免黑屏?

格尔茨

1 个答案:

答案 0 :(得分:1)

使用MPMoviePlayerController时,您将无法完全消除视频之间的预缓冲延迟(黑色阶段)。

改为使用AVQueuePlayer

AVQueuePlayerAVPlayer的子类,用于按顺序播放多个项目。

关于此事,请参阅其他questions and issues