Cache sounds before playing
parent
aaedb444c0
commit
32820d9d2f
|
@ -1,6 +1,7 @@
|
|||
1.5.7:
|
||||
- Added preset widgets
|
||||
- Removed "Set to silent" notification control setting (use "Alarms only" instead)
|
||||
- Resolve sound/vibration delays on some devices
|
||||
|
||||
1.5.6:
|
||||
- Sessions may now be imported from previous exports (CSV files)
|
||||
|
|
|
@ -8,8 +8,8 @@ android {
|
|||
minSdkVersion 16
|
||||
targetSdkVersion 28
|
||||
|
||||
versionCode 157
|
||||
versionName "1.5.7"
|
||||
versionCode 158
|
||||
versionName "1.5.8"
|
||||
|
||||
applicationId "sh.ftp.rocketninelabs.meditationassistant"
|
||||
manifestPlaceholders = [
|
||||
|
|
|
@ -142,12 +142,7 @@ public class CompleteActivity extends Activity {
|
|||
|
||||
String finishSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_finish", "");
|
||||
if (!manual && !finishSoundPath.equals("none")) {
|
||||
if (finishSoundPath.equals("custom")) {
|
||||
finishSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_finish_custom", "");
|
||||
getMeditationAssistant().playSound(0, finishSoundPath, true);
|
||||
} else {
|
||||
getMeditationAssistant().playSound(MeditationSounds.getMeditationSound(finishSoundPath), "", true);
|
||||
}
|
||||
getMeditationAssistant().playSessionSound(2, false);
|
||||
} else {
|
||||
getMeditationAssistant().restoreVolume();
|
||||
}
|
||||
|
|
|
@ -6,8 +6,10 @@ import android.content.DialogInterface;
|
|||
import android.content.SharedPreferences;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.content.res.TypedArray;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.preference.ListPreference;
|
||||
|
@ -216,7 +218,14 @@ public class ListPreferenceSound extends ListPreference {
|
|||
afd.getFileDescriptor(),
|
||||
afd.getStartOffset(),
|
||||
afd.getDeclaredLength());
|
||||
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
AudioAttributes audioAttributes = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
.build();
|
||||
mMediaPlayer.setAudioAttributes(audioAttributes);
|
||||
} else {
|
||||
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
}
|
||||
mMediaPlayer.setVolume(mediaVolume, mediaVolume);
|
||||
mMediaPlayer.prepareAsync();
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
|
|
@ -107,15 +107,9 @@ public class MainActivity extends Activity implements OnShowcaseEventListener {
|
|||
}
|
||||
|
||||
String intervalSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_interval", "");
|
||||
|
||||
if (!intervalSoundPath.equals("none") || getMeditationAssistant().vibrationEnabled()) {
|
||||
if (!intervalSoundPath.equals("none")) {
|
||||
if (intervalSoundPath.equals("custom")) {
|
||||
intervalSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_interval_custom", "");
|
||||
getMeditationAssistant().playSound(0, intervalSoundPath, false);
|
||||
} else {
|
||||
getMeditationAssistant().playSound(MeditationSounds.getMeditationSound(intervalSoundPath), "", false);
|
||||
}
|
||||
getMeditationAssistant().playSessionSound(1, false);
|
||||
}
|
||||
|
||||
getMeditationAssistant().vibrateDevice();
|
||||
|
@ -1224,6 +1218,9 @@ public class MainActivity extends Activity implements OnShowcaseEventListener {
|
|||
skipDelay = false;
|
||||
intervals = 0;
|
||||
|
||||
getMeditationAssistant().clearSoundCache();
|
||||
getMeditationAssistant().cacheSessionSounds();
|
||||
|
||||
Log.d("MeditationAssistant", "Timestamp: " + String.valueOf(timestamp));
|
||||
Integer secondsTillFinished = 0;
|
||||
if (getMeditationAssistant().getTimerMode().equals("timed")) {
|
||||
|
@ -1364,15 +1361,7 @@ public class MainActivity extends Activity implements OnShowcaseEventListener {
|
|||
getMeditationAssistant().vibrateDevice();
|
||||
}
|
||||
|
||||
String startSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_start", "");
|
||||
if (!startSoundPath.equals("none")) {
|
||||
if (startSoundPath.equals("custom")) {
|
||||
startSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_start_custom", "");
|
||||
getMeditationAssistant().playSound(0, startSoundPath, false);
|
||||
} else {
|
||||
getMeditationAssistant().playSound(MeditationSounds.getMeditationSound(startSoundPath), "", false);
|
||||
}
|
||||
}
|
||||
getMeditationAssistant().playSessionSound(0, false);
|
||||
|
||||
setIntervalAlarm();
|
||||
}
|
||||
|
@ -1844,18 +1833,12 @@ public class MainActivity extends Activity implements OnShowcaseEventListener {
|
|||
}
|
||||
|
||||
String intervalSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_interval", "");
|
||||
|
||||
if (!intervalSoundPath.equals("none") || getMeditationAssistant().vibrationEnabled()) {
|
||||
if (getMeditationAssistant().getTimeToStopMeditate() == -1
|
||||
|| ((System.currentTimeMillis() / 1000) > getMeditationAssistant().getTimeToStopMeditate() && (System.currentTimeMillis() / 1000) - getMeditationAssistant().getTimeToStopMeditate() >= 5) || getMeditationAssistant().getTimeToStopMeditate()
|
||||
- (System.currentTimeMillis() / 1000) >= 5) { // Not within last 5 seconds
|
||||
if (!intervalSoundPath.equals("none")) {
|
||||
if (intervalSoundPath.equals("custom")) {
|
||||
intervalSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_interval_custom", "");
|
||||
getMeditationAssistant().playSound(0, intervalSoundPath, false);
|
||||
} else {
|
||||
getMeditationAssistant().playSound(MeditationSounds.getMeditationSound(intervalSoundPath), "", false);
|
||||
}
|
||||
getMeditationAssistant().playSessionSound(1, false);
|
||||
}
|
||||
|
||||
getMeditationAssistant().vibrateDevice();
|
||||
|
@ -1902,15 +1885,7 @@ public class MainActivity extends Activity implements OnShowcaseEventListener {
|
|||
|
||||
if (fullWakeUp) {
|
||||
if (getMeditationAssistant().getPrefs().getBoolean("pref_softfinish", false)) {
|
||||
String finishSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_finish", "");
|
||||
if (!finishSoundPath.equals("none")) {
|
||||
if (finishSoundPath.equals("custom")) {
|
||||
finishSoundPath = getMeditationAssistant().getPrefs().getString("pref_meditation_sound_finish_custom", "");
|
||||
getMeditationAssistant().playSound(0, finishSoundPath, true);
|
||||
} else {
|
||||
getMeditationAssistant().playSound(MeditationSounds.getMeditationSound(finishSoundPath), "", true);
|
||||
}
|
||||
}
|
||||
getMeditationAssistant().playSessionSound(2, false);
|
||||
|
||||
getMeditationAssistant().vibrateDevice();
|
||||
} else {
|
||||
|
|
|
@ -16,6 +16,7 @@ import android.content.Intent;
|
|||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.net.Uri;
|
||||
|
@ -139,6 +140,7 @@ public class MeditationAssistant extends Application {
|
|||
private WakeLocker wakeLocker = new WakeLocker();
|
||||
String pausedTimerHoursMinutes;
|
||||
String pausedTimerSeconds;
|
||||
private HashMap<String, MediaPlayer> mediaPlayers = new HashMap<String, MediaPlayer>();
|
||||
|
||||
private AlertDialog sessionDialog = null;
|
||||
private int sessionDialogStartedYear = -1;
|
||||
|
@ -505,17 +507,33 @@ public class MeditationAssistant extends Application {
|
|||
return medinetprovider;
|
||||
}
|
||||
|
||||
public void playSound(int soundresource, String soundpath, boolean restoreVolume) {
|
||||
String wakeLockID = acquireWakeLock(false);
|
||||
public void cacheSound(int soundresource, String soundpath) {
|
||||
String cacheKey = soundpath;
|
||||
if (cacheKey.equals("")) {
|
||||
cacheKey = Integer.toString(soundresource);
|
||||
}
|
||||
if (mediaPlayers.containsKey(cacheKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
MediaPlayer mp = new MediaPlayer();
|
||||
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
AudioAttributes audioAttributes = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.build();
|
||||
mp.setAudioAttributes(audioAttributes);
|
||||
} else {
|
||||
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
|
||||
}
|
||||
try {
|
||||
if (!soundpath.equals("")) {
|
||||
mp.setDataSource(getApplicationContext(), Uri.parse(soundpath));
|
||||
} else {
|
||||
mp.setDataSource(getApplicationContext(), Uri.parse("android.resource://" + getPackageName() + "/" + soundresource));
|
||||
}
|
||||
|
||||
mp.prepareAsync();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
|
@ -524,11 +542,76 @@ public class MeditationAssistant extends Application {
|
|||
soundLabel = String.valueOf(soundresource);
|
||||
}
|
||||
Log.e("MeditationAssistant", "Failed to load sound: " + soundLabel);
|
||||
return;
|
||||
}
|
||||
|
||||
if (restoreVolume) {
|
||||
restoreVolume();
|
||||
mediaPlayers.put(cacheKey, mp);
|
||||
}
|
||||
|
||||
public void cacheSessionSounds() {
|
||||
String label;
|
||||
for (int i = 0;i < 3;i++) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
label = "start";
|
||||
break;
|
||||
case 1:
|
||||
label = "interval";
|
||||
break;
|
||||
case 2:
|
||||
label = "finish";
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences prefs = getPrefs();
|
||||
String soundPath = prefs.getString("pref_meditation_sound_" + label, "");
|
||||
if (!soundPath.equals("none")) {
|
||||
if (soundPath.equals("custom")) {
|
||||
cacheSound(0, prefs.getString("pref_meditation_sound_" + label + "_custom", ""));
|
||||
} else {
|
||||
cacheSound(MeditationSounds.getMeditationSound(soundPath), "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clearSoundCache() {
|
||||
for (MediaPlayer mp : mediaPlayers.values()) {
|
||||
try {
|
||||
mp.stop();
|
||||
} catch (Exception e) {
|
||||
// Do nothing
|
||||
}
|
||||
try {
|
||||
mp.release();
|
||||
} catch (Exception e) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
mediaPlayers.clear();
|
||||
}
|
||||
|
||||
public void playSound(int soundresource, String soundpath, boolean restoreVolume) {
|
||||
String wakeLockID = acquireWakeLock(false);
|
||||
|
||||
String cacheKey = soundpath;
|
||||
if (cacheKey.equals("")) {
|
||||
cacheKey = Integer.toString(soundresource);
|
||||
}
|
||||
if (!mediaPlayers.containsKey(cacheKey)) {
|
||||
cacheSound(soundresource, soundpath);
|
||||
}
|
||||
if (!mediaPlayers.containsKey(cacheKey)) {
|
||||
Log.d("MeditationAssistant", "Failed to cache sound");
|
||||
return; // Failed to load sound
|
||||
}
|
||||
MediaPlayer mp = mediaPlayers.get(cacheKey);
|
||||
|
||||
if (mp.isPlaying()) {
|
||||
Log.d("MeditationAssistant", "Failed to play sound: already playing");
|
||||
releaseWakeLock(wakeLockID);
|
||||
return;
|
||||
}
|
||||
|
@ -540,20 +623,24 @@ public class MeditationAssistant extends Application {
|
|||
MeditationAssistant.this.restoreVolume();
|
||||
}
|
||||
|
||||
mp.release();
|
||||
try {
|
||||
mp.stop();
|
||||
} catch (Exception e) {
|
||||
// Do nothing
|
||||
}
|
||||
try {
|
||||
mp.prepareAsync();
|
||||
} catch (Exception e) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
MeditationAssistant.this.releaseWakeLock(wakeLockID);
|
||||
}
|
||||
});
|
||||
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
||||
@Override
|
||||
public void onPrepared(MediaPlayer mp) {
|
||||
mp.start();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
mp.prepare();
|
||||
} catch (IOException e) {
|
||||
mp.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
if (restoreVolume) {
|
||||
|
@ -564,6 +651,33 @@ public class MeditationAssistant extends Application {
|
|||
}
|
||||
}
|
||||
|
||||
public void playSessionSound(int sound, boolean restoreVolume) {
|
||||
String label;
|
||||
switch (sound) {
|
||||
case 0:
|
||||
label = "start";
|
||||
break;
|
||||
case 1:
|
||||
label = "interval";
|
||||
break;
|
||||
case 2:
|
||||
label = "finish";
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences prefs = getPrefs();
|
||||
String soundPath = prefs.getString("pref_meditation_sound_" + label, "");
|
||||
if (!soundPath.equals("none")) {
|
||||
if (soundPath.equals("custom")) {
|
||||
playSound(0, prefs.getString("pref_meditation_sound_" + label + "_custom", ""), restoreVolume);
|
||||
} else {
|
||||
playSound(MeditationSounds.getMeditationSound(soundPath), "", restoreVolume);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void startAuth(Context context, boolean showToast) {
|
||||
String trace = Arrays.toString(Thread.currentThread().getStackTrace());
|
||||
Log.d("MeditationAssistant", "startAuth called, current stack trace: " + trace);
|
||||
|
|
Loading…
Reference in New Issue