Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix release()-ing of items from the callback pool #476

Merged
merged 3 commits into from
Feb 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions RNSound/RNSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ -(NSString *) getDirectory:(int)directory {

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer*)player
successfully:(BOOL)flag {
NSNumber* key = [self keyForPlayer:player];
if (key == nil) return;
@synchronized(self) {
NSNumber* key = [self keyForPlayer:player];
if (key == nil) return;

@synchronized(key) {
[self setOnPlay:NO forPlayerKey:key];
RCTResponseSenderBlock callback = [self callbackForKey:key];
if (callback) {
Expand Down Expand Up @@ -204,12 +204,14 @@ -(NSDictionary *)constantsToExport {
}

if (player) {
player.delegate = self;
player.enableRate = YES;
[player prepareToPlay];
[[self playerPool] setObject:player forKey:key];
callback(@[[NSNull null], @{@"duration": @(player.duration),
@"numberOfChannels": @(player.numberOfChannels)}]);
@synchronized(self) {
player.delegate = self;
player.enableRate = YES;
[player prepareToPlay];
[[self playerPool] setObject:player forKey:key];
callback(@[[NSNull null], @{@"duration": @(player.duration),
@"numberOfChannels": @(player.numberOfChannels)}]);
}
} else {
callback(@[RCTJSErrorFromNSError(error)]);
}
Expand Down Expand Up @@ -245,13 +247,15 @@ -(NSDictionary *)constantsToExport {
}

RCT_EXPORT_METHOD(release:(nonnull NSNumber*)key) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[player stop];
[[self callbackPool] removeObjectForKey:player];
[[self playerPool] removeObjectForKey:key];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
@synchronized(self) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[player stop];
[[self callbackPool] removeObjectForKey:key];
[[self playerPool] removeObjectForKey:key];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
}
}

Expand Down