Skip to content

Commit

Permalink
feat: add support to change playback speed (#619)
Browse files Browse the repository at this point in the history
support to change playback speed!

@todo: #619 (review)
  • Loading branch information
DanielFRico authored Sep 3, 2024
1 parent d725be0 commit 978dd9c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ class RNAudioRecorderPlayerModule(private val reactContext: ReactApplicationCont
promise.resolve("set volume")
}

@ReactMethod
fun setPlaybackSpeed(playbackSpeed: Float, promise: Promise) {
if (mediaPlayer == null) {
promise.reject("setPlaybackSpeed", "player is null.")
return
}
mediaPlayer!!.playbackParams = mediaPlayer!!.playbackParams.setSpeed(playbackSpeed)
promise.resolve("setPlaybackSpeed")
}

@ReactMethod
fun startPlayer(path: String, httpHeaders: ReadableMap?, promise: Promise) {
if (mediaPlayer != null) {
Expand Down
9 changes: 9 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ class AudioRecorderPlayer {
return RNAudioRecorderPlayer.setVolume(volume);
};

/**
* Set playback speed.
* @param {number} setPlaybackSpeed set playback speed.
* @returns {Promise<string>}
*/
setPlaybackSpeed = async (playbackSpeed: number): Promise<string> => {
return RNAudioRecorderPlayer.setPlaybackSpeed(playbackSpeed);
};

/**
* Set subscription duration. Default is 0.5.
* @param {number} sec subscription callback duration in seconds.
Expand Down
4 changes: 4 additions & 0 deletions ios/RNAudioRecorderPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ @interface RCT_EXTERN_MODULE(RNAudioRecorderPlayer, RCTEventEmitter)
resolve:(RCTPromiseResolveBlock) resolve
rejecter:(RCTPromiseRejectBlock) reject);

RCT_EXTERN_METHOD(setPlaybackSpeed:(float)playbackSpeed
resolve:(RCTPromiseResolveBlock) resolve
rejecter:(RCTPromiseRejectBlock) reject);

RCT_EXTERN_METHOD(startPlayer:(NSString*)path
httpHeaders:(NSDictionary*)httpHeaders
resolve:(RCTPromiseResolveBlock)resolve
Expand Down
15 changes: 15 additions & 0 deletions ios/RNAudioRecorderPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,21 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
audioPlayer.volume = volume
resolve(volume)
}

@objc(setPlaybackSpeed:resolve:rejecter:)
public func setPlaybackSpeed(
playbackSpeed: Float,
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) -> Void {
if (audioPlayer == nil) {
return reject("RNAudioPlayerRecorder", "Player is null", nil)
}

audioPlayer.rate = playbackSpeed
resolve("setPlaybackSpeed")
}

private func avFormat(fromString encoding: String?) -> AudioFormatID? {
if (encoding == nil) {
return kAudioFormatAppleLossless
Expand Down

0 comments on commit 978dd9c

Please sign in to comment.