Skip to content

Expose isPlaying via getInfo #179

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ Set the volume of the current player. This does not change the volume of the dev

**Android**: 0 will play the sound once. Any other number will loop indefinitely until the `stop()` command is called.

### `getInfo() => Promise<{currentTime: number, duration: number}>`
### `getInfo() => Promise<{currentTime: number, duration: number, isPlaying: boolean}>`

Get the `currentTime` and `duration` of the currently mounted audio media. This function returns a promise which resolves to an Object containing `currentTime` and `duration` properties.
Get the `currentTime`, `duration` and `isPlaying` status of the currently mounted audio media. This function returns a promise which resolves to an Object containing `currentTime`, `duration` and `isPlaying` properties.

```javascript
// Example
Expand All @@ -210,7 +210,7 @@ Get the `currentTime` and `duration` of the currently mounted audio media. This
async getInfo() { // You need the keyword `async`
try {
const info = await SoundPlayer.getInfo() // Also, you need to await this because it is async
console.log('getInfo', info) // {duration: 12.416, currentTime: 7.691}
console.log('getInfo', info) // {duration: 12.416, currentTime: 7.691, isPlaying: true}
} catch (e) {
console.log('There is no song playing', e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void getInfo(
WritableMap map = Arguments.createMap();
map.putDouble("currentTime", this.mediaPlayer.getCurrentPosition() / 1000.0);
map.putDouble("duration", this.mediaPlayer.getDuration() / 1000.0);
map.putBoolean("isPlaying", this.mediaPlayer.isPlaying());
promise.resolve(map);
}

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare module "react-native-sound-player" {
/** iOS: 0 means to play the sound once, a positive number specifies the number of times to return to the start and play again, a negative number indicates an indefinite loop. Android: 0 means to play the sound once, other numbers indicate an indefinite loop. */
setNumberOfLoops: (loops: number) => void;
/** Get the currentTime and duration of the currently mounted audio media. This function returns a promise which resolves to an Object containing currentTime and duration properties. */
getInfo: () => Promise<{ currentTime: number; duration: number }>;
getInfo: () => Promise<{ currentTime: number; duration: number; isPlaying: boolean }>;
/** @deprecated Please use addEventListener and remove your own listener by calling yourSubscriptionObject.remove(). */
unmount: () => void;
}
Expand Down
13 changes: 11 additions & 2 deletions ios/RNSoundPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,24 @@ -(void)stopObserving {
if (self.player != nil) {
NSDictionary *data = @{
@"currentTime": [NSNumber numberWithDouble:[self.player currentTime]],
@"duration": [NSNumber numberWithDouble:[self.player duration]]
@"duration": [NSNumber numberWithDouble:[self.player duration]],
@"isPlaying": [NSNumber numberWithBool:[self.player isPlaying]]
};
resolve(data);
} else if (self.avPlayer != nil) {
CMTime currentTime = [[self.avPlayer currentItem] currentTime];
CMTime duration = [[[self.avPlayer currentItem] asset] duration];
BOOL isPlaying = NO;
if (@available(iOS 10.0, *)) {
isPlaying = (self.avPlayer.timeControlStatus == AVPlayerTimeControlStatusPlaying);
} else {
isPlaying = (self.avPlayer.rate != 0.0f);
}

NSDictionary *data = @{
@"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(currentTime)],
@"duration": [NSNumber numberWithFloat:CMTimeGetSeconds(duration)]
@"duration": [NSNumber numberWithFloat:CMTimeGetSeconds(duration)],
@"isPlaying": [NSNumber numberWithBool:isPlaying]
};
resolve(data);
} else {
Expand Down