From 88e1663ef05db685f143b63f9681be14be787d6f Mon Sep 17 00:00:00 2001 From: surya-rd Date: Thu, 29 May 2025 11:52:23 -0500 Subject: [PATCH] Expose isPlaying via getInfo --- README.md | 6 +++--- .../rnsoundplayer/RNSoundPlayerModule.java | 1 + index.d.ts | 2 +- ios/RNSoundPlayer.m | 13 +++++++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 90ef414..64cc962 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) } diff --git a/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java b/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java index ee9c3cd..28050d7 100644 --- a/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java +++ b/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java @@ -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); } diff --git a/index.d.ts b/index.d.ts index b3d9a93..49fe192 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; } diff --git a/ios/RNSoundPlayer.m b/ios/RNSoundPlayer.m index bef6d87..a408bc7 100644 --- a/ios/RNSoundPlayer.m +++ b/ios/RNSoundPlayer.m @@ -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 {