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

[iOS] Added setSpeed method for controlling playback speed #173

Open
asRizvi888 opened this issue Aug 20, 2024 · 0 comments
Open

[iOS] Added setSpeed method for controlling playback speed #173

asRizvi888 opened this issue Aug 20, 2024 · 0 comments

Comments

@asRizvi888
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java b/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java
index f8c4f2a..d6432c4 100644
--- a/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java
+++ b/node_modules/react-native-sound-player/android/src/main/java/com/johnsonsu/rnsoundplayer/RNSoundPlayerModule.java
@@ -36,6 +36,7 @@ public class RNSoundPlayerModule extends ReactContextBaseJavaModule implements L
   private MediaPlayer mediaPlayer;
   private float volume;
   private AudioManager audioManager;
+  private float speed = 1.0f;
 
   public RNSoundPlayerModule(ReactApplicationContext reactContext) {
     super(reactContext);
@@ -103,10 +104,19 @@ public class RNSoundPlayerModule extends ReactContextBaseJavaModule implements L
     }
   }
 
+  // @ReactMethod
+  // public void resume() throws IOException, IllegalStateException {
+  //   if (this.mediaPlayer != null) {
+  //     this.setVolume(this.volume);
+  //     this.mediaPlayer.start();
+  //   }
+  // }
+
   @ReactMethod
   public void resume() throws IOException, IllegalStateException {
     if (this.mediaPlayer != null) {
       this.setVolume(this.volume);
+      this.setSpeed(this.speed);
       this.mediaPlayer.start();
     }
   }
@@ -133,6 +143,14 @@ public class RNSoundPlayerModule extends ReactContextBaseJavaModule implements L
     }
   }
 
+  @ReactMethod
+  public void setSpeed(float speed) throws IOException {
+    this.speed = speed;
+    if (this.mediaPlayer != null) {
+      this.mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed));
+    }
+  }
+
   @ReactMethod
   public void getInfo(
           Promise promise) {
diff --git a/node_modules/react-native-sound-player/index.d.ts b/node_modules/react-native-sound-player/index.d.ts
index 5b72f4c..c7fc782 100644
--- a/node_modules/react-native-sound-player/index.d.ts
+++ b/node_modules/react-native-sound-player/index.d.ts
@@ -50,6 +50,8 @@ declare module "react-native-sound-player" {
     setMixAudio: (on: boolean) => void;
     /** IOS only. Set the number of loops. A negative value will loop indefinitely until the stop() command is called. */
     setNumberOfLoops: (loops: number) => void;
+    /**Set playback speed, Default is set to 1 */
+    setSpeed: (speed: 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 }>;
     /** @deprecated Please use addEventListener and remove your own listener by calling yourSubscriptionObject.remove(). */
diff --git a/node_modules/react-native-sound-player/index.js b/node_modules/react-native-sound-player/index.js
index f15fdb3..c983151 100644
--- a/node_modules/react-native-sound-player/index.js
+++ b/node_modules/react-native-sound-player/index.js
@@ -123,6 +123,10 @@ export default {
     }
   },
 
+  setSpeed: (volume: number) => {
+     RNSoundPlayer.setSpeed(volume);
+  },
+
   getInfo: async () => RNSoundPlayer.getInfo(),
 
   unmount: () => {
diff --git a/node_modules/react-native-sound-player/ios/RNSoundPlayer.m b/node_modules/react-native-sound-player/ios/RNSoundPlayer.m
index bef6d87..478521d 100644
--- a/node_modules/react-native-sound-player/ios/RNSoundPlayer.m
+++ b/node_modules/react-native-sound-player/ios/RNSoundPlayer.m
@@ -168,6 +168,16 @@ -(void)stopObserving {
     }
 }
 
+RCT_EXPORT_METHOD(setSpeed:(float)speed) {
+    if (self.player != nil) {
+        self.player.enableRate = YES;
+        self.player.rate = speed;
+    } 
+    if (self.avPlayer != nil) {
+        self.avPlayer.rate = speed;
+    }
+}
+
 RCT_REMAP_METHOD(getInfo,
                  getInfoWithResolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {

This issue body was partially generated by patch-package.

@asRizvi888 asRizvi888 changed the title Added setSpeed method for controlling playback speed (ios) [iOS] Added setSpeed method for controlling playback speed Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant