Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Catch IllegalStateException on local file playback #205

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not mean we never invoke our callback? Can we detect the state?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK that exception is only thrown if the preparation has already been done. Actual errors should still happen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but callback would never be invoked, which means react native is never informed. Unless onPrepared is invoked each time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and detecting the state isn't possible. There's no 'isPrepared' or 'getState' on MediaPlayer :-(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If onPrepared isn't invoked, we could split the creation to a file based media player, and an http/s based one

// 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