Skip to content

Commit

Permalink
Fix crash from trying to remove observer multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Vatne committed May 30, 2015
1 parent ca3e606 commit c5eb6b4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Examples/VideoPlayer/package.json
Expand Up @@ -6,7 +6,7 @@
"start": "node_modules/react-native/packager/packager.sh"
},
"dependencies": {
"react-native": "^0.4.0",
"react-native-video": "^0.3.5"
"react-native": "^0.4.4",
"react-native-video": "^0.4.1"
}
}
27 changes: 23 additions & 4 deletions RCTVideo.m
Expand Up @@ -18,6 +18,7 @@ @implementation RCTVideo
{
AVPlayer *_player;
AVPlayerItem *_playerItem;
BOOL _playerItemObserverSet;
AVPlayerLayer *_playerLayer;
NSURL *_videoURL;

Expand Down Expand Up @@ -87,18 +88,36 @@ - (void)startProgressTimer {
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

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

- (void)addPlayerItemObserver
{
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
_playerItemObserverSet = YES;
}

/* Fixes https://github.com/brentvatne/react-native-video/issues/43
* Crashes caused when trying to remove the observer when there is no
* observer set */
- (void)removePlayerItemObserver
{
if (_playerItemObserverSet) {
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
_playerItemObserverSet = NO;
}
}

#pragma mark - Player and source

- (void)setSrc:(NSDictionary *)source {
[_playerItem removeObserver:self forKeyPath:statusKeyPath];
[self removePlayerItemObserver];
_playerItem = [self playerItemForSource:source];
[_playerItem addObserver:self forKeyPath:statusKeyPath options:0 context:nil];
[self addPlayerItemObserver];

[_player pause];
[_playerLayer removeFromSuperlayer];
Expand Down Expand Up @@ -321,7 +340,7 @@ - (void)removeFromSuperview {
[_playerLayer removeFromSuperlayer];
_playerLayer = nil;

[_playerItem removeObserver:self forKeyPath:statusKeyPath];
[self removePlayerItemObserver];

_eventDispatcher = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "react-native-video",
"version": "0.4.0",
"version": "0.4.1",
"description": "A <Video /> element for react-native",
"main": "Video.ios.js",
"author": "Brent Vatne <brentvatne@gmail.com> (https://github.com/brentvatne)",
Expand Down

0 comments on commit c5eb6b4

Please sign in to comment.