Skip to content

Commit ff7a633

Browse files
authored
Android: Catch IllegalStateException when trying to play file-based audio. (#205)
1 parent 04ab715 commit ff7a633

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

android/src/main/java/com/zmxv/RNSound/RNSoundModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.facebook.react.bridge.ReactMethod;
1414
import com.facebook.react.bridge.ReadableMap;
1515
import com.facebook.react.bridge.WritableMap;
16+
import com.facebook.react.modules.core.ExceptionsManagerModule;
1617

1718
import java.io.File;
1819
import java.util.HashMap;
@@ -87,7 +88,13 @@ public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
8788
return true;
8889
}
8990
});
90-
player.prepareAsync();
91+
92+
try {
93+
player.prepareAsync();
94+
} catch (IllegalStateException ignored) {
95+
// When loading files from a file, we useMediaPlayer.create, which actually
96+
// prepares the audio for us already. So we catch and ignore this error
97+
}
9198
}
9299

93100
protected MediaPlayer createMediaPlayer(final String fileName) {
@@ -111,6 +118,7 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
111118
File file = new File(fileName);
112119
if (file.exists()) {
113120
Uri uri = Uri.fromFile(file);
121+
// Mediaplayer is already prepared here.
114122
return MediaPlayer.create(this.context, uri);
115123
}
116124
return null;

0 commit comments

Comments
 (0)