I created an AVPlayerViewController
and an attached AVPlayer
in the viewDidAppear
method of a custom UIViewController
. Ho
Can't believe nobody introduced the most obvious solution: just fire a notification or do whatever you need in AVPlayerViewController
's -dealloc
method. Just don't hold a strong
reference to it, otherwise -dealloc
won't be called.
There's nothing wrong with subclassing AVPlayerViewController
(even if docs say otherwise), if you don't break internal logic (e.g. not calling super
from overridden methods). I've been using the following code for years (since iOS 8 came out) without any runtime or AppStore submission issues:
@interface VideoPlayerViewController : AVPlayerViewController
@end
@implementation VideoPlayerViewController
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; }
@end
// now use VideoPlayerViewController instead of AVPlayerViewController
So, to answer the question, just add this to AVPlayerViewController
subclass:
- (void)dealloc {
// player was dismissed and is going to die, do any cleanup now
}
If you are really afraid to subclass, then use smart technique by attaching an associated object to AVPlayerViewController
, whose -dealloc
you can control, see here: https://stackoverflow.com/a/19344475
To comment on other solutions:
AVPlayer
's rate
becomes 0.0f
and check AVPlayerViewController
's isBeingDismissed
: clever, but fails when player is paused beforehand (doing it with timer instead works, of course)AVPlayerViewController
's -viewWillDisappear:
: might fail when something is presented on top of the player, e.g. built-in subtitle selector. A similar solution would be checking in -viewWillAppear:
of the presenting view controller if a player was presented before and should work 100% of the time.