Skip to content

Commit

Permalink
Fixed audio playback so it does not play when ending session after ap…
Browse files Browse the repository at this point in the history
…p returns to foreground
  • Loading branch information
tasota committed Jan 24, 2020
1 parent 80aea83 commit 92a87f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public abstract class AbstractActivity extends AppCompatActivity {

private final DelayedOnClickHandler delayedOnClickHandler = new DelayedOnClickHandler(this);
private boolean audioPlaybackPaused = false;


/**
Expand All @@ -41,12 +42,25 @@ public final boolean activityShouldHandleOnClickEvents() {
}


public synchronized void setAudioPlaybackPaused(boolean value) {
audioPlaybackPaused = value;
}


public boolean isAudioPlaybackPaused() {
return audioPlaybackPaused;
}


/**
* Hide navigation buttons to make the activity take up the entire screen.
*/
@Override
protected void onResume() {
super.onResume();
Log.i(Constants.LOG_TAG, "AbstractActivity.onResume");
AudioPlayer.getInstance(getApplicationContext()).stop();
setAudioPlaybackPaused(false);

View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
Expand All @@ -70,7 +84,8 @@ protected void onCreate(Bundle savedInstanceState) {
/** Makes sure the audio player is stopped before the app goes in the background. */
@Override
protected void onPause() {
Log.i(Constants.LOG_TAG, "onPause");
Log.i(Constants.LOG_TAG, "AbstractActivity.onPause");
setAudioPlaybackPaused(true);

AudioPlayer.getInstance(getApplicationContext()).stop();
if (activityUsesDelayedOnClickHandler()) {
Expand All @@ -93,9 +108,13 @@ public void playAudio(String filepath) {
* @param listener Callback for when the added audio finishes playing
*/
public void playAudio(String filepath, MediaPlayer.OnCompletionListener listener) {
AudioPlayer audioPlayer = AudioPlayer.getInstance(getApplicationContext());
audioPlayer.stop();
if (isAudioPlaybackPaused()) {
Log.w(Constants.LOG_TAG, "playAudio called from activity but audioPlaybackPaused is true; not playing audio.");
return;
}
if (filepath != null) {
AudioPlayer audioPlayer = AudioPlayer.getInstance(getApplicationContext());
audioPlayer.stop();
audioPlayer.addAudioFromAssets(filepath, listener);
audioPlayer.playAudio();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ private void playAudioFile() {
@Override
public void onChanged(@Nullable DbFile dbFile) {
// TODO check type?
if (isAudioPlaybackPaused()) {
Log.w(Constants.LOG_TAG, "onChanged trying to add/play audio from activity but audioPlaybackPaused is true; not adding/playing audio.");
return;
}
audioPlayer.addAudioFromAssets(dbFile.getFilePath());
audioPlayer.playAudio();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.cmucreatelab.android.flutterprek.activities.student_section.coping_skills.post_coping_skills;

import android.media.MediaPlayer;
import android.util.Log;

import org.cmucreatelab.android.flutterprek.Constants;
import org.cmucreatelab.android.flutterprek.activities.student_section.StudentSectionActivityWithTimeout;

public abstract class PostCopingSkillActivity extends StudentSectionActivityWithTimeout implements MediaPlayer.OnCompletionListener {
Expand All @@ -10,6 +12,7 @@ public abstract class PostCopingSkillActivity extends StudentSectionActivityWith
@Override
protected void onResume() {
super.onResume();
Log.i(Constants.LOG_TAG, "PostCopingSkillActivity.onResume");
playAudio(getAudioFileForPostCopingSkillTitle(), this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.cmucreatelab.android.flutterprek.activities.student_section.coping_skills.post_coping_skills.post_coping_skill_heart_beating.fragments.HeartBeatingFragment;
import org.cmucreatelab.android.flutterprek.activities.student_section.coping_skills.post_coping_skills.post_coping_skill_heart_beating.fragments.HowFastIsHeartBeatingFragment;
import org.cmucreatelab.android.flutterprek.activities.student_section.coping_skills.post_coping_skills.post_coping_skill_heart_beating.fragments.PlaceHandOnHeartFragment;
import org.cmucreatelab.android.flutterprek.audio.AudioPlayer;

import static org.cmucreatelab.android.flutterprek.SessionTracker.ITINERARY_INDEX;

Expand Down Expand Up @@ -48,6 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
this.howFastIsHeartBeatingFragment = (HowFastIsHeartBeatingFragment) (getSupportFragmentManager().findFragmentById(R.id.howFastIsHeartBeatingFragment));
}


@Override
public void setFragment(HeartBeatingFragment.FragmentState fragmentState) {
String audioToPlay;
Expand All @@ -60,7 +60,6 @@ public void setFragment(HeartBeatingFragment.FragmentState fragmentState) {
howFastIsHeartBeatingFragment.displayFragment(true, this);
audioToPlay = "etc/audio_prompts/audio_how_fast_is_heart_beating.wav";
}
AudioPlayer.getInstance(getApplicationContext()).stop();
playAudio(audioToPlay);
restartOverlayTimers();
}
Expand All @@ -73,6 +72,7 @@ public void goToNextActivity() {
finish();
}


@Override
protected void onResume() {
setFragment(HeartBeatingFragment.FragmentState.PLACE_HAND_ON_HEART);
Expand Down

0 comments on commit 92a87f8

Please sign in to comment.