A strange issue with MediaPlayer on Android version 9 - null pointer exception

0

Recently I changed my smartphone to Xiaomi Redmi Note 7 (Android 9) and wanted to test one of my applications on it. The application does not start because there is some problem with MediaPlayer. Which does not appear on any smartphone or tablet on which I tested the application(even on much weaker than note 7). However, they all worked on the older version of the android.

Errors:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: aaa.aaa.aa, PID: 25635
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference
                  at aaa.aaa.aa.MyActivity.prepare_media_minigun(MyActivity.java:6789)
                  at aaa.aaa.aa.MyActivity.onClick(MyActivity.java:4176)
                  at android.view.View.performClick(View.java:6608)
                  at android.view.View.performClickInternal(View.java:6585)
                  at android.view.View.access$3100(View.java:785)
                  at android.view.View$PerformClick.run(View.java:25921)
                  at android.os.Handler.handleCallback(Handler.java:873)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:201)
                  at android.app.ActivityThread.main(ActivityThread.java:6810)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)


E/MediaPlayerNative: Unable to create media player
D/MediaPlayer: create failed:
           java.io.IOException: setDataSourceFD failed.: status=0x80000000
               at android.media.MediaPlayer._setDataSource(Native Method)
               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1304)
               at android.media.MediaPlayer.create(MediaPlayer.java:998)
               at android.media.MediaPlayer.create(MediaPlayer.java:971)

My code:

static void prepare_media_minigun() {
    for (int i = 0; i < minigun_max_ammo; i++) {
        media_minigun[i] = new MediaPlayer();

        if (minigun_silencer == false) {
            media_minigun[i] = MediaPlayer.create(context, R.raw.minigun);
        } else {
            media_minigun[i] = MediaPlayer.create(context, R.raw.minigun2);
        }

        media_minigun[i].setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }

    minigun_player_unloaded = false;
}

Array of MediaPlayers:

public static   MediaPlayer[] media_minigun = new MediaPlayer[100] ;

The prepare_media_minigun() method is called in OnCreate().

Maybe someone has already encountered that error, because I do not have any ideas how to solve it. This seems to be a problem with loading sounds from the raw folder.

I installed this application on OnePlus 7 with android 9.0 and it also does not work. It seems that the problem is something in this version of android.

android
android-studio
nullpointerexception
android-mediaplayer
android-9.0-pie
asked on Stack Overflow Jul 30, 2019 by pawel22 • edited Nov 24, 2020 by Metrier

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0