AVPlayerviewcontroller is not dismissing

435 views Asked by At

I am doing one application. In that I used the AVPlayerViewController to play the video. But its not dismiss after play the video.

Please check the my source:

 _player = [AVPlayer playerWithURL:self.videoURL];
        _avVideoController = [[AVPlayerViewController alloc]init];
        _avVideoController.view.frame = self.view.frame;
        _avVideoController.delegate =self;
        _avVideoController.player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
        _avVideoController.player = _player;
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[_avVideoController.player currentItem]];
        [_player play];
        [self.view addSubview:_avVideoController.view];
        [self presentViewController:_avVideoController animated:YES completion:nil];


- (void)playerItemDidReachEnd:(NSNotification *)notification
{

[_avVideoController dismissViewControllerAnimated:YES completion:nil];
[_avVideoController.view removeFromSuperview];
NSLog(@"IT REACHED THE END");

 }

After this AVPlayerViewController is not dismissing.

1

There are 1 answers

2
Luzo On

Not hundred percent sure, but might be because of NSNotificationCenter still keeps a reference to self. Try to remove observer in - (void)playerItemDidReachEnd:(NSNotification *)notification function. As of that

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:AVPlayerItemDidPlayToEndTimeNotification 
                                              object:nil];