Skip to content

Commit

Permalink
Fix attachListeners method's threading issue & other notification han…
Browse files Browse the repository at this point in the history
…dling issues
  • Loading branch information
isair authored and brentvatne committed Jun 25, 2015
1 parent 17595f9 commit 44b1711
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ - (void)startProgressTimer
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)notifyEnd:(NSNotification *)notification
{
[_eventDispatcher sendInputEventWithName:RNVideoEventEnd body:@{
@"target": self.reactTag
}];
}

- (void)addPlayerItemObserver
{
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
Expand Down Expand Up @@ -237,19 +230,25 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)attachListeners
{
dispatch_async(dispatch_get_main_queue(), ^{
// listen for end of file
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notifyEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];

selector:@selector(playerItemDidEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
});
}

- (void)playerItemDidReachEnd:(NSNotification *)notification
- (void)playerItemDidEnd:(NSNotification *)notification
{
[_eventDispatcher sendInputEventWithName:RNVideoEventEnd body:@{
@"target": self.reactTag
}];
if (_repeat) {
AVPlayerItem *item = [notification object];
[item seekToTime:kCMTimeZero];
[self applyModifiers];
}
}

#pragma mark - Prop setters
Expand Down Expand Up @@ -342,27 +341,8 @@ - (void)applyModifiers
[self setPaused:_paused];
}

- (void)setRepeatEnabled
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
}

- (void)setRepeatDisabled
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setRepeat:(BOOL)repeat {
_repeat = repeat;

if (repeat) {
[self setRepeatEnabled];
} else {
[self setRepeatDisabled];
}
}

#pragma mark - React View Management
Expand Down

0 comments on commit 44b1711

Please sign in to comment.