0

I followed a youtube tutorial that showed how to create an android mp3 player app. The App has multiple buttons, one for previous song, one for Pause/Play and one for next song. Play/Pause buttons, only pause my MediaPlayer and they work perfectly no matter how many times i click them.

On the other hand, Next/Previous buttons causes my code to throw NullPointerException. I found out it was because MediaPlayer.create(Context, Uri) function sometimes returns null. On my app, function fails approx. on every tenth press of Next button, or on every tenth creation of MediaPlayer.

Is there a way to fix this? I tried modifying application on my own, but it didn't work. I tried looping variable myMysicPlayer in continue while loop, until it gets while != null

while(myMusicPlayer != null){
 Uri u = new Uri(...);
 myMediaPlayer = MediaPlayer.create(getAppContext(), u);
}

I tried using a listener that waits till myMusicPlayer was ready to be stopped, but it was just dereferencing null, and that failed obviosely.

btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMediaPlayer.stop();
myMediaPlayer.reset();
myMediaPlayer.release();
position = (position + 1) % mySongs.size();
Uri u = Uri.parse(mySongs.get(position).toString());                    
songLabelName.setText(mySongs.get(position).toString());    
myMediaPlayer = MediaPlayer.create(getApplicationContext(), u);
myMediaPlayer.start();    
}
});

The error message:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.aizenangel.myapplication, PID: 20169
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference            at com.aizenangel.myapplication.SongPlayer$4.onClick(SongPlayer.java:168)
    at android.view.View.performClick(View.java:6291)
    at android.view.View$PerformClick.run(View.java:24931)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
    I/Process: Sending signal. PID: 20169 SIG: 9
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
AizenAngel
  • 11
  • 3
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Aug 06 '19 at 15:13
  • You should not ever be creating more than 1 MediaPlayer per Activity. These are large, expensive objects. Creating a lot of them causes problems. These should instead be shared between multiple uses of media player. – Gabe Sechan Aug 06 '19 at 16:04
  • Can i play more than one song on the same MediaPlayer? I mean can i create MediaPlayer instance only once and than just switch between songs, without releasing MediaPlayer object (like i've done so far)? – AizenAngel Aug 07 '19 at 12:06

1 Answers1

0

My mistake was:
1) i used myMediaPlayer.release() method, that releases all resources associated with the MediaPlayer object
2) instead of using the same MediaPlayer object, i always create new one with myMediaPlayer = MediaPlayer.create(Context, Uri), and MediaPlayer.create(Context, Uri) returns null on failure, which than causes NullPointerException.

Solution was simple. I deleted myMediaPlayer.release() line and instead of creating new object, i modified the one whose reference i got in the begining.

btnNext.setOnClickListener(new View.OnClickListener() {
  public void onClick(View view) {
   myMediaPlayer.stop();
   myMediaPlayer.reset();
   position = (position+1)%mySongs.size();
   Uri u = Uri.parse((mySongs.get(position)).toString());
   songLabelName.setText(mySongs.get(position).toString());
   try {
    myMediaPlayer.setDataSource(getApplicationContext(),u);
    myMediaPlayer.prepare();
   }catch (IOException e) {
    e.printStackTrace();
   }
   myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
     myMediaPlayer.start();
    }
   });
  }
});
AizenAngel
  • 11
  • 3