johnsonsu/react-native-sound-player

[BUG] NPE while setting (android.media.MediaPlayer$OnCompletionListener)' on a null object reference

Opened this issue · 8 comments

Describe the bug
We see crashes on multiple devices in production when using the SoundPlayer.loadUrl despite adding a try-catch block around it.

Here is the crash log:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference
at com.johnsonsu.rnsoundplayer.RNSoundPlayerModule.prepareUrl(RNSoundPlayerModule.java:23)
at com.johnsonsu.rnsoundplayer.RNSoundPlayerModule.loadUrl(RNSoundPlayerModule.java)
at java.lang.reflect.Method.invoke(Method.java)
at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:149)
at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:21)
at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java)
at android.os.Looper.loop(Looper.java:223)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:37)
at java.lang.Thread.run(Thread.java:923)

To Reproduce
So far I have been unable to reproduce this but I still continue to see in our crashlytics logs.

Expected behavior
A clear and concise description of what you expected to happen.

Platform (please complete the following information):

  • OS: Android

Additional context
Add any other context about the problem here.

I notice that this error happens when currentActivity in null and thus this.mediaPlayer = MediaPlayer.create(getCurrentActivity(), uri); still returns an empty object in RNSoundPlayerModule.java.
Can we please add an additional check to proceed only if mediaPlayer is non-null?

Sample code to fix the NPE :
` private void prepareUrl(final String url) throws IOException {
if (this.mediaPlayer == null) {
Uri uri = Uri.parse(url);
this.mediaPlayer = MediaPlayer.create(getCurrentActivity(), uri);

  // In cases when getCurrentActivity is null, it throws an NPE for setting listeners on empty object.
  if (this.mediaPlayer == null) return

  this.mediaPlayer.setOnCompletionListener(
    new OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer arg0) {
        WritableMap params = Arguments.createMap();
        params.putBoolean("success", true);
        sendEvent(getReactApplicationContext(), EVENT_FINISHED_PLAYING, params);
      }
  });
  this.mediaPlayer.setOnPreparedListener(
    new OnPreparedListener() {
      @Override
      public void onPrepared(MediaPlayer mediaPlayer) {
        WritableMap onFinishedLoadingURLParams = Arguments.createMap();
        onFinishedLoadingURLParams.putBoolean("success", true);
        onFinishedLoadingURLParams.putString("url", url);
        sendEvent(getReactApplicationContext(), EVENT_FINISHED_LOADING_URL, onFinishedLoadingURLParams);
      }
    }
  );
} else {
  Uri uri = Uri.parse(url);
  this.mediaPlayer.reset();
  this.mediaPlayer.setDataSource(getCurrentActivity(), uri);
  this.mediaPlayer.prepareAsync();
}
WritableMap params = Arguments.createMap();
params.putBoolean("success", true);
sendEvent(getReactApplicationContext(), EVENT_FINISHED_LOADING, params);

}`

Any solves?
(OS: Android 10)

Log:

Also having this issue, we cannot implement this library for android in our enterprise product without a fix for this, and therefore this bug is a major blocker to our next app release. Please patch this ASAP.

hi any update on it?

Any update on this issue .I am also facing this issue.

Same issue for me in production as well.

Any time a user on a specific device plays a sound (app uses SoundPlayer.playSoundFile()), the app crashes. The devices that regularly see this are:

  • LG Q6
  • LG V30
  • LG V60 ThinQ
  • LG K51
  • LG L455DL

Same issue in 2023 in unity 2022.3.7f1

Same issue for me, any updates on it?