Skip to content

Commit

Permalink
Android: Catch IllegalStateException when trying to play file-based a…
Browse files Browse the repository at this point in the history
…udio. (#205)
  • Loading branch information
benvium committed Jun 16, 2017
1 parent 04ab715 commit ff7a633
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.ExceptionsManagerModule;

import java.io.File;
import java.util.HashMap;
Expand Down Expand Up @@ -87,7 +88,13 @@ public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
player.prepareAsync();

try {
player.prepareAsync();
} catch (IllegalStateException ignored) {
// When loading files from a file, we useMediaPlayer.create, which actually
// prepares the audio for us already. So we catch and ignore this error
}
}

protected MediaPlayer createMediaPlayer(final String fileName) {
Expand All @@ -111,6 +118,7 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
File file = new File(fileName);
if (file.exists()) {
Uri uri = Uri.fromFile(file);
// Mediaplayer is already prepared here.
return MediaPlayer.create(this.context, uri);
}
return null;
Expand Down

0 comments on commit ff7a633

Please sign in to comment.