Skip to content

Commit cd7b335

Browse files
committed
Do not crash when LoudnessEnhancer is not initialized for some reason
1 parent c160428 commit cd7b335

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111
applicationId "com.brouken.player"
1212
minSdkVersion 21
1313
targetSdkVersion 34
14-
versionCode 167
14+
versionCode 168
1515
versionName "0.${versionCode}"
1616
archivesBaseName = "Just.Player.v${versionName}"
1717
}

app/src/main/java/com/brouken/player/PlayerActivity.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,12 +1293,12 @@ public void initializePlayer() {
12931293
}
12941294
player.setMediaItem(mediaItemBuilder.build(), mPrefs.getPosition());
12951295

1296-
if (loudnessEnhancer != null) {
1297-
loudnessEnhancer.release();
1298-
}
12991296
try {
1297+
if (loudnessEnhancer != null) {
1298+
loudnessEnhancer.release();
1299+
}
13001300
loudnessEnhancer = new LoudnessEnhancer(player.getAudioSessionId());
1301-
} catch (RuntimeException e) {
1301+
} catch (Exception e) {
13021302
e.printStackTrace();
13031303
}
13041304

@@ -1407,12 +1407,12 @@ public void releasePlayer(boolean save) {
14071407
private class PlayerListener implements Player.Listener {
14081408
@Override
14091409
public void onAudioSessionIdChanged(int audioSessionId) {
1410-
if (loudnessEnhancer != null) {
1411-
loudnessEnhancer.release();
1412-
}
14131410
try {
1411+
if (loudnessEnhancer != null) {
1412+
loudnessEnhancer.release();
1413+
}
14141414
loudnessEnhancer = new LoudnessEnhancer(audioSessionId);
1415-
} catch (RuntimeException e) {
1415+
} catch (Exception e) {
14161416
e.printStackTrace();
14171417
}
14181418
notifyAudioSessionUpdate(true);

app/src/main/java/com/brouken/player/Utils.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,23 @@ public static void adjustVolume(final Context context, final AudioManager audioM
190190
PlayerActivity.boostLevel = 0;
191191
}
192192

193-
if (PlayerActivity.loudnessEnhancer == null)
193+
try {
194+
if (PlayerActivity.loudnessEnhancer == null || !PlayerActivity.loudnessEnhancer.hasControl()) {
195+
canBoost = false;
196+
}
197+
} catch (Exception e) {
194198
canBoost = false;
199+
e.printStackTrace();
200+
}
195201

196202
if (volume != volumeMax || (PlayerActivity.boostLevel == 0 && !raise)) {
197-
if (PlayerActivity.loudnessEnhancer != null)
198-
PlayerActivity.loudnessEnhancer.setEnabled(false);
203+
if (PlayerActivity.loudnessEnhancer != null) {
204+
try {
205+
PlayerActivity.loudnessEnhancer.setEnabled(false);
206+
} catch (Exception e) {
207+
e.printStackTrace();
208+
}
209+
}
199210
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, raise ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
200211
final int volumeNew = getVolume(context, false, audioManager);
201212
// Custom volume step on Samsung devices (Sound Assistant)
@@ -219,16 +230,21 @@ else if (!raise && PlayerActivity.boostLevel > 0)
219230
if (PlayerActivity.loudnessEnhancer != null) {
220231
try {
221232
PlayerActivity.loudnessEnhancer.setTargetGain(PlayerActivity.boostLevel * 200);
222-
} catch (RuntimeException e) {
233+
} catch (Exception e) {
223234
e.printStackTrace();
224235
}
225236
}
226237
playerView.setCustomErrorMessage(" " + (volumeMax + PlayerActivity.boostLevel));
227238
}
228239

229240
playerView.setIconVolume(volumeActive);
230-
if (PlayerActivity.loudnessEnhancer != null)
231-
PlayerActivity.loudnessEnhancer.setEnabled(PlayerActivity.boostLevel > 0);
241+
if (PlayerActivity.loudnessEnhancer != null) {
242+
try {
243+
PlayerActivity.loudnessEnhancer.setEnabled(PlayerActivity.boostLevel > 0);
244+
} catch (Exception e) {
245+
e.printStackTrace();
246+
}
247+
}
232248
playerView.setHighlight(PlayerActivity.boostLevel > 0);
233249

234250
if (clear) {

0 commit comments

Comments
 (0)