If I change the orientation of the phone the music would keep playing which is good but it won't pause or stop either.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_music);
musicState = String.valueOf(player.getState());
if (savedInstanceState != null)
{
musicState = savedInstanceState.getString("State");
}
player.state = MP3Player.MP3PlayerState.valueOf(musicState);
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("Red", r);
outState.putInt("Green", g);
outState.putInt("Blue", b);
outState.putFloat("SPEED", playbackspeed);
outState.putInt("PROGRESS", progressUpdate);
outState.putInt("Update", updateIncrement);
outState.putInt("Duration", duration);
outState.putString("State", musicState);
}
public void onMusicBackClick ( View v ) {
Log. d (" g53mdp ", " button pressed !") ;
Intent intent = new Intent(CurrentMusic.this, MainActivity.class);
player.stop();
intent.putExtra("Red", r);
intent.putExtra("Green", g);
intent.putExtra("Blue", b);
intent.putExtra("SPEED", playbackspeed);
startActivity(intent);
finish();
}
public void onPlayClick (View v)
{
Log.d("MP3 Player", "Music Play button pressed");
if(player.getState() == MP3Player.MP3PlayerState.PAUSED)
{
player.play();
musicState = String.valueOf(player.getState());
}
else if(player.getState() == MP3Player.MP3PlayerState.PLAYING)
{
player.pause();
musicState = String.valueOf(player.getState());
}
else
{
player.load(musicURL, playbackspeed);
musicState = String.valueOf(player.getState());
updateIncrement = player.getDuration()/100;
duration = player.getDuration();
musicProgress = findViewById(R.id.p_musicProgress);
musicProgress.setMax(duration);
progress = player.getProgress();
timer.scheduleAtFixedRate(timerTask = new TimerTask(){
@Override
public void run() {
progress = player.getProgress();
progressUpdate = progress;
musicProgress.setProgress(progress);
}
}, 0, updateIncrement);
}
}
public void onPauseClick (View v)
{
Log.d("MP3 Player", "Music pause button pressed");
if(player.getState() == MP3Player.MP3PlayerState.PLAYING)
{
player.pause();
}
}
public void onStopClick (View v)
{
Log.d("MP3 Player", "Music stop button pressed");
if(player.getState() == MP3Player.MP3PlayerState.PAUSED || player.getState() == MP3Player.MP3PlayerState.PLAYING)
{
player.stop();
}
player.stop();
}
I thought just converting values to string and then extracting it and putting it back would work but if you play the media file and orientated the phone it almost goes rogue.
I'm printing out states and am checking every state under every single interaction and even after recreating the activity after orientation it says and tells me its actually stopped but its still some how playing?
Your problem is the reference to the MP3 player instance is not persisting when the activity is recreated, but continues to exist in the background since it's playing music. The correct way to persist anything through the activity lifecycle is to use a ViewModel, you should create one and store your MP3 player instance there.