Skip to content

Commit

Permalink
Added callbacks to stop and pause, because the RN bridge is asynchron…
Browse files Browse the repository at this point in the history
…ous (#167)
  • Loading branch information
ndbroadbent authored and benvium committed May 12, 2017
1 parent 4f75daa commit 1a725fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ whoosh.getCurrentTime((seconds) => console.log('at ' + seconds));
whoosh.pause();

// Stop the sound and rewind to the beginning
whoosh.stop();
whoosh.stop(() => {
// Note: If you want to play a sound after stopping and rewinding it,
// it is important to call play() in a callback.
whoosh.play();
});

// Release the audio player resource
whoosh.release();
Expand All @@ -196,13 +200,17 @@ whoosh.release();
Return `true` if the sound has been loaded.

### `play(onEnd)`
`onEnd` {?function(successfully)} Optinoal callback function that gets called when the playback finishes successfully or an audio decoding error interrupts it.
`onEnd` {?function(successfully)} Optional callback function that gets called when the playback finishes successfully or an audio decoding error interrupts it.

### `pause(callback)`
`callback` {?function()} Optional callback function that gets called when the sound has been paused.

### `pause()`
Pause the sound.

### `stop()`
Stop the playback.
### `stop(callback)`
`callback` {?function()} Optional callback function that gets called when the sound has been stopped.

Stop playback and set the seek position to 0.

### `release()`
Release the audio player resource associated with the instance.
Expand Down
6 changes: 4 additions & 2 deletions RNSound/RNSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,20 @@ -(NSDictionary *)constantsToExport {
}
}

RCT_EXPORT_METHOD(pause:(nonnull NSNumber*)key) {
RCT_EXPORT_METHOD(pause:(nonnull NSNumber*)key withCallback:(RCTResponseSenderBlock)callback) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[player pause];
callback(@[]);
}
}

RCT_EXPORT_METHOD(stop:(nonnull NSNumber*)key) {
RCT_EXPORT_METHOD(stop:(nonnull NSNumber*)key withCallback:(RCTResponseSenderBlock)callback) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[player stop];
player.currentTime = 0;
callback(@[]);
}
}

Expand Down
6 changes: 4 additions & 2 deletions android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,22 @@ public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
}

@ReactMethod
public void pause(final Integer key) {
public void pause(final Integer key, final Callback callback) {
MediaPlayer player = this.playerPool.get(key);
if (player != null && player.isPlaying()) {
player.pause();
}
callback.invoke();
}

@ReactMethod
public void stop(final Integer key) {
public void stop(final Integer key, final Callback callback) {
MediaPlayer player = this.playerPool.get(key);
if (player != null && player.isPlaying()) {
player.pause();
player.seekTo(0);
}
callback.invoke();
}

@ReactMethod
Expand Down
8 changes: 4 additions & 4 deletions sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ Sound.prototype.play = function(onEnd) {
return this;
};

Sound.prototype.pause = function() {
Sound.prototype.pause = function(callback) {
if (this._loaded) {
RNSound.pause(this._key);
RNSound.pause(this._key, () => { callback && callback() });
}
return this;
};

Sound.prototype.stop = function() {
Sound.prototype.stop = function(callback) {
if (this._loaded) {
RNSound.stop(this._key);
RNSound.stop(this._key, () => { callback && callback() });
}
return this;
};
Expand Down
6 changes: 4 additions & 2 deletions windows/RNSoundModule/RNSoundModule/RNSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void play(int key, ICallback callback)
}

[ReactMethod]
public void pause(int key)
public void pause(int key, ICallback callback)
{
MediaPlayer player = null;

Expand All @@ -190,10 +190,11 @@ public void pause(int key)
{
player.Pause();
}
callback.Invoke();
}

[ReactMethod]
public void stop(int key)
public void stop(int key, ICallback callback)
{
MediaPlayer player = null;

Expand All @@ -204,6 +205,7 @@ public void stop(int key)

player.Pause();
player.PlaybackSession.Position = new TimeSpan(0);
callback.Invoke();
}

[ReactMethod]
Expand Down

0 comments on commit 1a725fd

Please sign in to comment.