Skip to content

Commit

Permalink
Changes from react-native-sound
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehedman committed Nov 8, 2015
1 parent 48b66a2 commit 0393e1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
63 changes: 30 additions & 33 deletions RNSimpleSound/RNSimpleSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,51 @@
#import <AVFoundation/AVFoundation.h>

@implementation RNSimpleSound {
NSMutableArray* _playerPool;
BOOL _enabled;
}

-(NSMutableArray*) playerPool {
if (!_playerPool) {
_playerPool = [[NSMutableArray alloc] init];
}
return _playerPool;
BOOL _enabled;
AVAudioPlayer* player;
}

-(NSURL*) soundURL:(NSString*)fileName {
return [[NSBundle mainBundle] URLForResource:[[fileName lastPathComponent]stringByDeletingPathExtension]
withExtension:[fileName pathExtension]];
return [[NSBundle mainBundle] URLForResource:[[fileName lastPathComponent]stringByDeletingPathExtension]
withExtension:[fileName pathExtension]];
}

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(enable:(BOOL)enabled) {
_enabled = enabled;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory: AVAudioSessionCategoryAmbient error: nil];
[session setActive: enabled error: nil];
_enabled = enabled;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory: AVAudioSessionCategoryAmbient error: nil];
[session setActive: enabled error: nil];
}

RCT_EXPORT_METHOD(prepare:(NSString *)fileName) {
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:[self soundURL:fileName] error:nil];
[player prepareToPlay];
[[self playerPool] addObject:player];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[self soundURL:fileName] error:nil];
[player prepareToPlay];
}

RCT_EXPORT_METHOD(play:(NSString *)fileName) {
RCT_EXPORT_METHOD(play) {
if (!_enabled || !player.url) {
return;
}

[player play];
}

RCT_EXPORT_METHOD(pause) {
if (!_enabled) {
return;
}
[player pause];
}

RCT_EXPORT_METHOD(stop) {
if (!_enabled) {
return;
}

NSURL *soundURL = [self soundURL:fileName];

for (AVAudioPlayer* player in [[self playerPool] mutableCopy]) {
if (!player.playing && [player.url isEqual:soundURL]) {
[player play];
return;
}
}

AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[player play];
[[self playerPool] addObject:player];
[player stop];
//stop does not reset the player, so force it
[player prepareToPlay];
}

@end
@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-simple-sound",
"version": "1.0.0",
"version": "1.0.1",
"description": "Start, stop, and pause a sound. iOS only. Derived from https://github.com/zmxv/react-native-sound",
"main": "simple_sound.js",
"scripts": {
Expand Down

0 comments on commit 0393e1f

Please sign in to comment.