Skip to content

Commit

Permalink
Fix callback crashes - Guarantee that the RN callback is only ever ca…
Browse files Browse the repository at this point in the history
…lled once (#168)

* Make sure the RN callback is only ever called once

* Fix callback crash on Windows
  • Loading branch information
ndbroadbent authored and benvium committed May 8, 2017
1 parent 31132db commit f31895b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
13 changes: 7 additions & 6 deletions RNSound/RNSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ -(NSString *) getDirectory:(int)directory {
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer*)player
successfully:(BOOL)flag {
NSNumber* key = [self keyForPlayer:player];
if (key != nil) {
@synchronized(key) {
RCTResponseSenderBlock callback = [self callbackForKey:key];
if (callback) {
callback(@[@(flag)]);
}
if (key == nil) return;

@synchronized(key) {
RCTResponseSenderBlock callback = [self callbackForKey:key];
if (callback) {
callback(@[@(flag)]);
[[self callbackPool] removeObjectForKey:key];
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,24 @@ public void play(final Integer key, final Callback callback) {
return;
}
player.setOnCompletionListener(new OnCompletionListener() {
boolean callbackWasCalled = false;

@Override
public synchronized void onCompletion(MediaPlayer mp) {
if (!mp.isLooping()) {
if (callbackWasCalled) return;
callbackWasCalled = true;
callback.invoke(true);
}
}
});
player.setOnErrorListener(new OnErrorListener() {
boolean callbackWasCalled = false;

@Override
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
if (callbackWasCalled) return true;
callbackWasCalled = true;
callback.invoke(false);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions windows/RNSoundModule/RNSoundModule/RNSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio
[ReactMethod]
public void play(int key, ICallback callback)
{
Boolean callbackWasCalled = false;
MediaPlayer player = null;
Debug.WriteLine("play()");

Expand All @@ -167,6 +168,8 @@ public void play(int key, ICallback callback)
player.MediaEnded +=
delegate
{
if (callbackWasCalled) return;
callbackWasCalled = true;
Debug.WriteLine("Media Ended");
callback.Invoke(true);
};
Expand Down

0 comments on commit f31895b

Please sign in to comment.