From 3ed5e18cc973d06a11135b83c2b23201097ea60b Mon Sep 17 00:00:00 2001 From: akshobhya Date: Fri, 14 Feb 2020 22:39:36 +0530 Subject: [PATCH 1/3] Fixes the media player in Meditation activity --- .../activities/MeditationActivity.java | 37 ++++++++++++++++--- .../main/output/audio/PlayerAdapter.java | 4 +- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/neurolab/activities/MeditationActivity.java b/app/src/main/java/io/neurolab/activities/MeditationActivity.java index c9842daf..1811eed8 100644 --- a/app/src/main/java/io/neurolab/activities/MeditationActivity.java +++ b/app/src/main/java/io/neurolab/activities/MeditationActivity.java @@ -21,6 +21,8 @@ public final class MeditationActivity extends AppCompatActivity { public static final String TAG = MeditationActivity.class.getCanonicalName(); // TAG for debugging purposes. public static int MEDIA_RES_ID; + private static String KEY_PLAYER = "PlayerAdapter_Key"; + private static String KEY_DURATIONVIEW = "DurationView_Key"; // Necessary view references. private SeekBar seekbarAudio; @@ -30,6 +32,7 @@ public final class MeditationActivity extends AppCompatActivity { // the interface reference which would be used to control the media session from this UI client. private PlayerAdapter playerAdapter; + private MediaPlayerHolder mediaPlayerHolder; private boolean isUserSeeking = false; @@ -41,24 +44,46 @@ protected void onCreate(Bundle savedInstanceState) { MEDIA_RES_ID = getIntent().getIntExtra(MEDITATION_DIR_KEY, R.raw.soften_and_relax); - grabNecessaryReferencesAndSetListeners(); + if (savedInstanceState == null) { + grabNecessaryReferencesAndSetListeners(); - setTrackName(MEDIA_RES_ID); + setTrackName(MEDIA_RES_ID); - initializePlaybackController(); + initializePlaybackController(); + + playerAdapter.loadMedia(MEDIA_RES_ID); + } + else { + grabNecessaryReferencesAndSetListeners(); + + setTrackName(MEDIA_RES_ID); + + durationView.setText(savedInstanceState.getCharSequence("Max")); + + mediaPlayerHolder=(MediaPlayerHolder)savedInstanceState.getSerializable(KEY_PLAYER); + mediaPlayerHolder.setPlaybackInfoListener(new PlaybackListener()); + mediaPlayerHolder.initializeProgressCallback(); + + playerAdapter=mediaPlayerHolder; + } + } + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putSerializable(KEY_PLAYER, mediaPlayerHolder); + outState.putCharSequence(KEY_DURATIONVIEW,durationView.getText()); } @Override protected void onStart() { super.onStart(); - playerAdapter.loadMedia(MEDIA_RES_ID); } @Override protected void onStop() { super.onStop(); // not releasing the media player resources during auto-rotation. I believe it won't make it logical for the user to listen to the audio again from the start if he rotates his phone. - if (isChangingConfigurations() && playerAdapter.isPlaying()) { + if (isChangingConfigurations()) { Log.d(TAG, "onStop: don't release MediaPlayer as screen is rotating & playing"); } else { playerAdapter.release(); @@ -114,7 +139,7 @@ private String getMeditationName(String rawName) { } private void initializePlaybackController() { - MediaPlayerHolder mediaPlayerHolder = new MediaPlayerHolder(this); + mediaPlayerHolder = new MediaPlayerHolder(this); Log.d(TAG, "Inside initializePlaybackController method: MediaPlayerHolder Created"); mediaPlayerHolder.setPlaybackInfoListener(new PlaybackListener()); playerAdapter = mediaPlayerHolder; diff --git a/app/src/main/java/io/neurolab/main/output/audio/PlayerAdapter.java b/app/src/main/java/io/neurolab/main/output/audio/PlayerAdapter.java index 257252a1..3ba2f80a 100644 --- a/app/src/main/java/io/neurolab/main/output/audio/PlayerAdapter.java +++ b/app/src/main/java/io/neurolab/main/output/audio/PlayerAdapter.java @@ -1,7 +1,9 @@ package io.neurolab.main.output.audio; +import java.io.Serializable; + // An Interface which allows the client Activity (containing the Media Controller UI) to control playback functions. -public interface PlayerAdapter { +public interface PlayerAdapter extends Serializable { void loadMedia(int resourceId); From 76981fc040f76c922d630d920a3b8a7e9d79555f Mon Sep 17 00:00:00 2001 From: akshobhya Date: Fri, 14 Feb 2020 22:48:29 +0530 Subject: [PATCH 2/3] Fixes the media player --- .../main/java/io/neurolab/activities/MeditationActivity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/neurolab/activities/MeditationActivity.java b/app/src/main/java/io/neurolab/activities/MeditationActivity.java index 1811eed8..762f10bb 100644 --- a/app/src/main/java/io/neurolab/activities/MeditationActivity.java +++ b/app/src/main/java/io/neurolab/activities/MeditationActivity.java @@ -60,11 +60,11 @@ protected void onCreate(Bundle savedInstanceState) { durationView.setText(savedInstanceState.getCharSequence("Max")); - mediaPlayerHolder=(MediaPlayerHolder)savedInstanceState.getSerializable(KEY_PLAYER); + mediaPlayerHolder = (MediaPlayerHolder)savedInstanceState.getSerializable(KEY_PLAYER); mediaPlayerHolder.setPlaybackInfoListener(new PlaybackListener()); mediaPlayerHolder.initializeProgressCallback(); - playerAdapter=mediaPlayerHolder; + playerAdapter = mediaPlayerHolder; } } @Override From d67f5eb04a5680df5cb6c3f729430b1a35c342c6 Mon Sep 17 00:00:00 2001 From: akshobhya Date: Fri, 14 Feb 2020 22:50:35 +0530 Subject: [PATCH 3/3] Fixes the media player --- .../main/java/io/neurolab/activities/MeditationActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/neurolab/activities/MeditationActivity.java b/app/src/main/java/io/neurolab/activities/MeditationActivity.java index 762f10bb..4fe790cd 100644 --- a/app/src/main/java/io/neurolab/activities/MeditationActivity.java +++ b/app/src/main/java/io/neurolab/activities/MeditationActivity.java @@ -58,7 +58,7 @@ protected void onCreate(Bundle savedInstanceState) { setTrackName(MEDIA_RES_ID); - durationView.setText(savedInstanceState.getCharSequence("Max")); + durationView.setText(savedInstanceState.getCharSequence(KEY_DURATIONVIEW)); mediaPlayerHolder = (MediaPlayerHolder)savedInstanceState.getSerializable(KEY_PLAYER); mediaPlayerHolder.setPlaybackInfoListener(new PlaybackListener());