Skip to content

Commit ceaed43

Browse files
authored
android-error-code-38 (react-native-audio-toolkit#223)
1 parent b4495ca commit ceaed43

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

android/src/main/java/com/reactnativecommunity/rctaudiotoolkit/AudioPlayerModule.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class AudioPlayerModule extends ReactContextBaseJavaModule implements Med
4141
Map<Integer, Boolean> playerAutoDestroy = new HashMap<>();
4242
Map<Integer, Boolean> playerContinueInBackground = new HashMap<>();
4343
Map<Integer, Callback> playerSeekCallback = new HashMap<>();
44+
Map<Integer, Float> playerSpeed = new HashMap<>();
4445

4546
boolean looping = false;
4647
private ReactApplicationContext context;
@@ -165,7 +166,7 @@ private Uri uriFromPath(String path) {
165166
if (file.exists()) {
166167
return Uri.fromFile(file);
167168
}
168-
169+
169170
// Try finding file in Android "raw" resources
170171
if (path.lastIndexOf('.') != -1) {
171172
fileNameWithoutExt = path.substring(0, path.lastIndexOf('.'));
@@ -174,7 +175,7 @@ private Uri uriFromPath(String path) {
174175
}
175176

176177
int resId = this.context.getResources().getIdentifier(fileNameWithoutExt,
177-
"raw", this.context.getPackageName());
178+
"raw", this.context.getPackageName());
178179
if (resId != 0) {
179180
return Uri.parse("android.resource://" + this.context.getPackageName() + "/" + resId);
180181
}
@@ -193,6 +194,7 @@ public void destroy(Integer playerId, Callback callback) {
193194
this.playerAutoDestroy.remove(playerId);
194195
this.playerContinueInBackground.remove(playerId);
195196
this.playerSeekCallback.remove(playerId);
197+
this.playerSpeed.remove(playerId);
196198

197199
WritableMap data = new WritableNativeMap();
198200
data.putString("message", "Destroyed player");
@@ -363,27 +365,20 @@ public void set(Integer playerId, ReadableMap options, Callback callback) {
363365
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (options.hasKey("speed") || options.hasKey("pitch"))) {
364366
PlaybackParams params = new PlaybackParams();
365367

366-
boolean needToPauseAfterSet = false;
367368
if (options.hasKey("speed") && !options.isNull("speed")) {
368-
// If the player wasn't already playing, then setting the speed value to a non-zero value
369-
// will start it playing and we don't want that so we need to make sure to pause it straight
370-
// after setting the speed value
371-
boolean wasAlreadyPlaying = player.isPlaying();
369+
// Since setSpeed should cause player to start,
370+
// in case player is not playing we store and apply it later
372371
float speedValue = (float) options.getDouble("speed");
373-
needToPauseAfterSet = !wasAlreadyPlaying && speedValue != 0.0f;
374-
375-
params.setSpeed(speedValue);
372+
this.playerSpeed.put(playerId,speedValue);
373+
//apply param only if isPlaying. If not, we defer it on start
374+
if(player.isPlaying()) params.setSpeed(speedValue);
376375
}
377376

378377
if (options.hasKey("pitch") && !options.isNull("pitch")) {
379378
params.setPitch((float) options.getDouble("pitch"));
380379
}
381380

382381
player.setPlaybackParams(params);
383-
384-
if (needToPauseAfterSet) {
385-
player.pause();
386-
}
387382
}
388383

389384
callback.invoke();
@@ -401,7 +396,23 @@ public void play(Integer playerId, Callback callback) {
401396
if (!this.mixWithOthers) {
402397
this.mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
403398
}
404-
player.start();
399+
400+
//lets start using setSpeed when supported.
401+
Float speedValue = this.playerSpeed.get(playerId);
402+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && speedValue != null) {
403+
PlaybackParams params = new PlaybackParams();
404+
params.setSpeed(speedValue);
405+
player.setPlaybackParams(params);
406+
407+
// check if device is honoring android spec: when setSpeed player should start
408+
// https://developer.android.com/reference/android/media/MediaPlayer#setPlaybackParams(android.media.PlaybackParams)
409+
// if not happen, explicitly call start
410+
if(!player.isPlaying()) {
411+
player.start();
412+
}
413+
} else {
414+
player.start();
415+
}
405416

406417
callback.invoke(null, getInfo(player));
407418
} catch (Exception e) {

0 commit comments

Comments
 (0)