So I have an application with a ListView that plays audio in one of the fragments. The issue is, that some users are reporting that the application stops playing audio after a certain amount of clicks. I know this is an issue that has to do with the MediaPlayer not being released correctly, but I cannot find a place where I am not releasing it. Hopefully you guys can help.
Here is where MediaPlayer is being used in MyAudioFragment.java. I am fairly certain that the issue is located in these lines of code.
MyAudioFragment.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(MainActivity.mediaPlayer != null && MainActivity.mediaPlayer.isPlaying()){
MainActivity.mediaPlayer.release();
MainActivity.mediaPlayer = new MediaPlayer();
}
MainActivity.mediaPlayer = MediaPlayer.create(getContext(), MainActivity.hashMap.get(adapterView.getItemAtPosition(i)));
MainActivity.mediaPlayer.start();
}
});
Here is where I deal with MediaPlayer in MainActivity.java
//...
public static MediaPlayer mediaPlayer;
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
//Layout stuff
mediaPlayer = new MediaPlayer();
//Setting up fragment stuff
//...
}
@Override
public void onPause(){
super.onPause();
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public void onResume(){
super.onResume();
mediaPlayer = new MediaPlayer();
}
Edit: Could it possibly be that I am not releasing it "OnCompletion" ?
On onCompletion, you must do release();