Skip to content

Commit

Permalink
Fix premature collection of MediaPlayer on Android
Browse files Browse the repository at this point in the history
If the MediaPlayer takes a while to prepare, it's possible that a GC
will cause the MediaPlayer to be collected between the time that
`prepareAsync` is called and the time that the `OnPreparedListener` is
invoked.

By moving the assignment of the `playerPool` reference to immediately
following the instantiation of the MediaPlayer, we eliminate the risk of
premature collection.
  • Loading branch information
jamesreggio committed Sep 29, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent cc107e5 commit 1227d55
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ public void prepare(final String fileName, final Integer key, final ReadableMap
e.putString("message", "resource not found");
return;
}
this.playerPool.put(key, player);

final RNSoundModule module = this;

@@ -59,7 +60,6 @@ public synchronized void onPrepared(MediaPlayer mp) {
if (callbackWasCalled) return;
callbackWasCalled = true;

module.playerPool.put(key, mp);
WritableMap props = Arguments.createMap();
props.putDouble("duration", mp.getDuration() * .001);
try {

0 comments on commit 1227d55

Please sign in to comment.