react-native-sound-player icon indicating copy to clipboard operation
react-native-sound-player copied to clipboard

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

Open avonipenti opened this issue 4 years ago • 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.

avonipenti avatar Jan 19 '21 15:01 avonipenti

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);

}`

avonipenti avatar Jan 21 '21 13:01 avonipenti

Any solves? (OS: Android 10)

Log:

vrolandd avatar Feb 10 '21 16:02 vrolandd

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.

BritCoder96 avatar Dec 13 '21 17:12 BritCoder96

hi any update on it?

princebhatia09 avatar Jan 09 '22 19:01 princebhatia09

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

muhamadjawad avatar Feb 02 '22 09:02 muhamadjawad

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

okcompewter avatar Apr 11 '22 17:04 okcompewter

Same issue in 2023 in unity 2022.3.7f1

VectorViral avatar Oct 21 '23 06:10 VectorViral

Same issue for me, any updates on it?

pixelsomatic avatar Mar 15 '24 12:03 pixelsomatic