播放多个视频时MPMoviePlayer不会显示

时间:2014-01-22 03:02:51

标签: ios mpmovieplayercontroller

在我的应用中,用户可以从相机胶卷中选择一个视频并在应用内播放。当我第一次选择视频时,一切都很好。但是,当视频结束并且我选择另一个视频时,我可以听到音频,但视频控制器从不在屏幕上显示。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    [self dismissViewControllerAnimated:NO completion:nil];

    [self Play];

}


-(void)Play
{


    self.player = [[MPMoviePlayerController alloc]initWithContentURL:self.videoURL];


    self.player.shouldAutoplay = YES;
    self.player.allowsAirPlay = YES;



    self.player.view.frame = self.view.frame;


    [self.view addSubview: self.player.view];
    [self.player setFullscreen:YES animated:YES];

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:self.player];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.player];


    [self.player play];

}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
    NSLog(@"MovieDone");

    [self.player.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:self.player];
       [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    [self resignFirstResponder];

    [self.navigationController popToRootViewControllerAnimated:YES];


}

- (void) exitedFullscreen:(NSNotification*) aNotification {
    NSLog(@"MovieDone");
    [self.player.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerDidExitFullscreenNotification
     object:self.player];

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    [self resignFirstResponder];

    [self.navigationController popToRootViewControllerAnimated:YES];


}

1 个答案:

答案 0 :(得分:0)

当你制作一个新的播放器时,你的另一个自我播放器会发生什么?

只是呆在那里并牢牢掌握你的资源。为self.player设置新值并不能摆脱现有的播放器。您可以通过通知在整个地方引用它。

在开始新玩家之前,你应该杀死所有的观察者。像这样:

- (void) movieFinishedCallback:(NSNotification*) aNotification {
    NSLog(@"MovieDone");

    [self.player.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]removeObserver:self];//kill all the observers here

    [self resignFirstResponder];

    [self.navigationController popToRootViewControllerAnimated:YES];


}