-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Automatically resume ASVideoNode after returning from background #3271
Automatically resume ASVideoNode after returning from background #3271
Conversation
AVPlayerLayer pauses video when the application goes into the background. This change resumes the video playing when the app returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -101,6 +101,9 @@ - (instancetype)initWithCache:(id<ASImageCacheProtocol>)cache downloader:(id<ASI | |||
[self addTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside]; | |||
_lastPlaybackTime = kCMTimeZero; | |||
|
|||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; | |||
[notificationCenter addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's be sure to remove ourself as an observer in dealloc
. Heads up, it's best to remove using the specific method removeObserver:name:object:
rather than simply removeObserver:
which is much slower 😱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, got into the habit since iOS9+ automatically deregisters, I'll add the remove step.
Source/ASVideoNode.mm
Outdated
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
|
||
if (_shouldBePlaying && ASInterfaceStateIncludesVisible(self.interfaceState)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid calling -interfaceState
while holding the lock, since that method takes the lock. Let's remove the explicit lock, and just call self.shouldBePlaying
to keep things simple. We can also use self.visible
as a shorthand for ASInterfaceStateIncludesVisible(self.interfaceState)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other places in ASVideoNode seem to behave the same is why I did it this way, happy to remove it.
- (void)applicationDidBecomeActive:(NSNotification *)notification | ||
{ | ||
if (self.shouldBePlaying && self.isVisible) { | ||
[self play]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the video is paused when the user backgrounds the app? Should we save that state and only start playing on resume if the video wasn't paused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is shouldBePlaying will be NO if the video is paused.
Hey, sorry we didn't get this in yet. AsyncDisplayKit is becoming Texture! Would you mind reopening your pull request there: https://github.com/TextureGroup/Texture/pulls More info about the rename here: https://medium.com/@Pinterest_Engineering/introducing-texture-a-new-home-for-asyncdisplaykit-e7c003308f50 |
Diff has moved! |
AVPlayerLayer pauses video when the application goes into the background. This change resumes the video playing when the app returns.
I'm not sure if there is a way to write tests for application state changes?