Skip to content

Commit

Permalink
Add setRate to android. Fix rate playback on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Buddy Reno committed Nov 15, 2018
1 parent 2acd3c2 commit d8cbfed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ Stops recording an audio file.
### Supported Platforms

- iOS
- Android

### Parameters

Expand Down
17 changes: 17 additions & 0 deletions src/android/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,23 @@ public void setVolume(String id, float volume) {
}
}

/**
* Set the playback rate of an audio file
*
* @param id The id of the audio player
* @param rate The playback rate
*/
public void setRate(String id, float rate) {
String TAG3 = "AudioHandler.setRate(): Error : ";
AudioPlayer audio = this.players.get(id);
if (audio != null) {
audio.setRate(rate);
} else {
LOG.e(TAG3, "Unknown Audio Player " + id);
}
}


private void onFirstPlayerCreated() {
origVolumeStream = cordova.getActivity().getVolumeControlStream();
cordova.getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
Expand Down
7 changes: 6 additions & 1 deletion src/ios/CDVSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ - (void)seekToAudio:(CDVInvokedUrlCommand*)command

BOOL isPlaying = (avPlayer.rate > 0 && !avPlayer.error);
BOOL isReadyToSeek = (avPlayer.status == AVPlayerStatusReadyToPlay) && (avPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay);
float currentPlaybackRate = avPlayer.rate;

// CB-10535:
// When dealing with remote files, we can get into a situation where we start playing before AVPlayer has had the time to buffer the file to be played.
Expand All @@ -590,7 +591,11 @@ - (void)seekToAudio:(CDVInvokedUrlCommand*)command
toleranceBefore: kCMTimeZero
toleranceAfter: kCMTimeZero
completionHandler: ^(BOOL finished) {
if (isPlaying) [avPlayer play];
if (isPlaying) {
[avPlayer play];
// [avPlayer play] sets the rate to 1, so we need to set it again after seeking
[avPlayer setRate:currentPlaybackRate];
};
}];
} else {
NSString* errMsg = @"AVPlayerItem cannot service a seek request with a completion handler until its status is AVPlayerItemStatusReadyToPlay.";
Expand Down

0 comments on commit d8cbfed

Please sign in to comment.