2014-01-22 46 views
0

在我的應用程序中,用戶可以從相機膠捲中選擇一個視頻並在應用程序中播放。當我第一次選擇一個視頻時,一切都很好。但是,當視頻結束並且我去選擇另一個視頻時,我可以聽到音頻,但視頻控制器從不在屏幕上顯示。MPMoviePlayer在播放多個視頻時不會顯示

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


} 

回答

0

當你製作一個新的self.player時發生了什麼?

它只是呆在那裏,並牢牢抓住您的資源。爲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]; 


} 
相關問題