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