In my Media Player app I use default media player and in my app song stops in between 10-13 min and that's starts from 0 again when click on pause icon. I have an issue with Media player always stopping randomly after some few mins. then I put secondary buffer progress I have check my audio is not buffering after some time that's why it is not play after that buffering min.
is there any solution to over come this issue? please help me for this as soon as possible.
if (mediaPlayer == null)
mediaPlayer = new MediaPlayer();
initMediaplyer();
if (mediaPlayer.isPlaying()) {
Log.e("Playinggggg", "stoppppp");
mediaPlayer.stop();
isMediaStart = false;
isPrepare = false;
isPause = false;
}
mediaPlayer = new MediaPlayer();
initMediaplyer();
mediaPlayer.setDataSource(url);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaPlayer.setAudioAttributes(
new AudioAttributes
.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build());
}
mediaPlayer.prepareAsync();
isPause = false;
isPrepare = true;
} catch (IllegalStateException | IOException e) {
FileDescriptor fileDescriptor1 = null;
setMediaPlayer("0", fileDescriptor1);
e.printStackTrace();
}
if (!mediaPlayer.isPlaying()) {
mediaPlayer.setOnPreparedListener(mp -> {
Log.e("Playinggggg", "Startinggg");
mediaPlayer.start();
isMediaStart = true;
isprogressbar = false;
setMediaPlaybackState(STATE_PLAYING);
mediaPlayer.setOnBufferingUpdateListener((mediaPlayer, i) -> {
binding.simpleSeekbar.setSecondaryProgress(i);
});
mediaPlayer.setOnCompletionListener(mediaPlayer -> {
if(mediaPlayer.isPlaying()){
callComplete();
}
});
mediaPlayer.setOnErrorListener((mediaPlayer, i, i1) -> {
switch (i) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
Log.d("MediaPlayer Error", "MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + i1);
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
Log.d("MediaPlayer Error", "MEDIA ERROR SERVER DIED " + i1);
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
Log.d("MediaPlayer Error", "MEDIA ERROR UNKNOWN " + i1);
break;
default:
Log.d("MediaPlayer Error", "not conform " + i1);
break;
}
return false;
});
});
}
}
if (isPause) {
binding.llPlay.setVisibility(View.VISIBLE);
binding.llPause.setVisibility(View.GONE);
buildNotification(PlaybackStatus.PAUSED, ctx, mainPlayModelList, addToQueueModelList, playFrom, position);
} else {
binding.llPause.setVisibility(View.VISIBLE);
binding.llPlay.setVisibility(View.GONE);
buildNotification(PlaybackStatus.PLAYING, ctx, mainPlayModelList, addToQueueModelList, playFrom, position);
}
private void initMediaplyer() {
try {
if (mediaSessionManager != null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSessionManager = (MediaSessionManager) ctx.getSystemService(Context.MEDIA_SESSION_SERVICE);
}
mediaSession = new MediaSessionCompat(ctx.getApplicationContext(), "AudioPlayer");
//Get MediaSessions transport controls
transportControls = mediaSession.getController().getTransportControls();
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaControllerCompatCallback = new MediaControllerCompat.Callback() {
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
super.onPlaybackStateChanged(state);
if( state == null ) {
return;
}
switch( state.getState() ) {
case PlaybackStateCompat.STATE_PLAYING: {
mCurrentState = STATE_PLAYING;
break;
}
case PlaybackStateCompat.STATE_PAUSED: {
mCurrentState = STATE_PAUSED;
break;
}
}
}
};
mediaSession.setCallback(new MediaSessionCompat.Callback() {
// Implement callbacks
@Override
public void onPlay() {
super.onPlay();
callPlay();
}
@Override
public void onPause() {
super.onPause();
callPause();
}
@Override
public void onSkipToNext() {
super.onSkipToNext();
if (!url.equalsIgnoreCase("")) {
callNext();
}
}
@Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
if (!url.equalsIgnoreCase("")) {
callPrevious();
}
}
@Override
public void onStop() {
super.onStop();
}
});
} catch (Exception e) {
Log.e("playwell init media err",e.getMessage());
e.printStackTrace();
}
}
}