1

I am trying to create Mediaplayer session with given uri. but it causes NullpointerException.

    Uri uri = Uri.parse(path);

    // Creating MediaPlayer with given song's URI
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
    }
    mediaPlayer = MediaPlayer.create(this, uri);

    try {
        // Setting the MediaPlayer Listener
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                seekBar.setMax(mp.getDuration());
                mediaPlayer.start();
                changeSeekbar();
            }
        });
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
    }

Given Logcat:

2020-04-07 22:21:05.289 12237-12237/com.example.musicappresearch2 E/Path: /storage/emulated/0/Music/Alone - Viren.mp3
2020-04-07 22:21:05.289 12237-12237/com.example.musicappresearch2 E/ERROR: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnPreparedListener(android.media.MediaPlayer$OnPreparedListener)' on a null object reference

Could you Tell me What i am doing wrong ? Thanks.

  • According to logs shared, your mediaPlayer object is not instantiated and it is null and hence application is giving NPE when tried to invoke setOnPreparedListener method on it. Can you add null check for mediaPlayer before invoking method on it and try. – Reeta Wani Apr 07 '20 at 18:28
  • Does this answer your question? [Mediaplayer prepare showing Illegal State Exception](https://stackoverflow.com/questions/35079310/mediaplayer-prepare-showing-illegal-state-exception) – VolkanSahin45 Apr 07 '20 at 18:37
  • This can help you https://developer.android.com/guide/topics/media/mediaplayer – VolkanSahin45 Apr 07 '20 at 18:41
  • None of theese helped. Actually this worked for API 23 but now in Android 10 it gives NPE – Iona Bartishvili Apr 07 '20 at 19:21
  • How are you getting the Uri? Through an intent? – Wrichik Basu Apr 07 '20 at 19:21
  • through Singleton (with Arraylist of SongModels). i tested and the patch was right before initializing mediaplayer. so have no idea why this is happening. – Iona Bartishvili Apr 07 '20 at 19:41

2 Answers2

1

There are two ways to write this code, both tested on the device

First of all, make sure you handle android.permission.READ_EXTERNAL_STORAGE correctly and you really have correct Uri.
MediaPlayer.create(this, uri); will fail if either context or uri are invalid.

MediaPlayer.create(this, uri); which itself already prepares player so you don't need .prepareAsync() in this situation. and your code is good to go.

another way:


    mediaPlayer = new MediaPlayer(); // hence, we don't use .create, manually instantiate
    try {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(this, uri);
        mediaPlayer.setOnPreparedListener(mp -> {
            mediaPlayer.start();
        });

        /* use async, if you don't want to block UI thread
         keep in mind, this should be called after setting listener  
       because it might prepare even until the listener has been set */
        mediaPlayer.prepareAsync(); 
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
    }

tatocaster
  • 316
  • 3
  • 10
0

Try this :

            Uri uri = Uri.parse(path);

        mediaPlayer = new MediaPlayer();
        try {
           // mediaPlayer.setDataSource(String.valueOf(uri));
           mediaPlayer.setDataSource(MainActivity.this,uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
Shimaa Yasser
  • 587
  • 4
  • 12