@@ -41,6 +41,7 @@ public class AudioPlayerModule extends ReactContextBaseJavaModule implements Med
41
41
Map <Integer , Boolean > playerAutoDestroy = new HashMap <>();
42
42
Map <Integer , Boolean > playerContinueInBackground = new HashMap <>();
43
43
Map <Integer , Callback > playerSeekCallback = new HashMap <>();
44
+ Map <Integer , Float > playerSpeed = new HashMap <>();
44
45
45
46
boolean looping = false ;
46
47
private ReactApplicationContext context ;
@@ -165,7 +166,7 @@ private Uri uriFromPath(String path) {
165
166
if (file .exists ()) {
166
167
return Uri .fromFile (file );
167
168
}
168
-
169
+
169
170
// Try finding file in Android "raw" resources
170
171
if (path .lastIndexOf ('.' ) != -1 ) {
171
172
fileNameWithoutExt = path .substring (0 , path .lastIndexOf ('.' ));
@@ -174,7 +175,7 @@ private Uri uriFromPath(String path) {
174
175
}
175
176
176
177
int resId = this .context .getResources ().getIdentifier (fileNameWithoutExt ,
177
- "raw" , this .context .getPackageName ());
178
+ "raw" , this .context .getPackageName ());
178
179
if (resId != 0 ) {
179
180
return Uri .parse ("android.resource://" + this .context .getPackageName () + "/" + resId );
180
181
}
@@ -193,6 +194,7 @@ public void destroy(Integer playerId, Callback callback) {
193
194
this .playerAutoDestroy .remove (playerId );
194
195
this .playerContinueInBackground .remove (playerId );
195
196
this .playerSeekCallback .remove (playerId );
197
+ this .playerSpeed .remove (playerId );
196
198
197
199
WritableMap data = new WritableNativeMap ();
198
200
data .putString ("message" , "Destroyed player" );
@@ -363,27 +365,20 @@ public void set(Integer playerId, ReadableMap options, Callback callback) {
363
365
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M && (options .hasKey ("speed" ) || options .hasKey ("pitch" ))) {
364
366
PlaybackParams params = new PlaybackParams ();
365
367
366
- boolean needToPauseAfterSet = false ;
367
368
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
372
371
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 );
376
375
}
377
376
378
377
if (options .hasKey ("pitch" ) && !options .isNull ("pitch" )) {
379
378
params .setPitch ((float ) options .getDouble ("pitch" ));
380
379
}
381
380
382
381
player .setPlaybackParams (params );
383
-
384
- if (needToPauseAfterSet ) {
385
- player .pause ();
386
- }
387
382
}
388
383
389
384
callback .invoke ();
@@ -401,7 +396,23 @@ public void play(Integer playerId, Callback callback) {
401
396
if (!this .mixWithOthers ) {
402
397
this .mAudioManager .requestAudioFocus (this , AudioManager .STREAM_MUSIC , AudioManager .AUDIOFOCUS_GAIN );
403
398
}
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
+ }
405
416
406
417
callback .invoke (null , getInfo (player ));
407
418
} catch (Exception e ) {
0 commit comments