简体   繁体   中英

AVPlayerviewcontroller is not dismissing

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM