Skip to content

Commit 4afbb7e

Browse files
committed
HXAudioPlayerUtils: Added try/catch to handle any additional exceptions that may occur when attempting to access the AudioManager.
1 parent 0a2870d commit 4afbb7e

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525

2626
</application>
2727

28-
</manifest>
28+
</manifest>

hxaudio/src/main/java/com/huhx0015/hxaudio/utils/HXAudioPlayerUtils.java

+25-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.media.AudioManager;
55
import android.os.Build;
6+
import android.util.Log;
67

78
/** -----------------------------------------------------------------------------------------------
89
* [HXAudioPlayerUtils] CLASS
@@ -13,32 +14,41 @@
1314

1415
public class HXAudioPlayerUtils {
1516

17+
/** CLASS VARIABLES ________________________________________________________________________ **/
18+
19+
// LOGGING VARIABLES
20+
private static final String LOG_TAG = HXAudioPlayerUtils.class.getSimpleName();
21+
1622
/** UTILITY METHODS ________________________________________________________________________ **/
1723

1824
// enableSystemSound(): Enables or disables the device's system sound effects. This is most
1925
// commonly used if physical button's sound effects need to be enabled/disabled.
20-
// Note: Android 7.0 and above requires "android.permission.ACCESS_NOTIFICATION_POLICY".
26+
// NOTE: Android 7.0 and above requires "android.permission.ACCESS_NOTIFICATION_POLICY".
2127
public static void enableSystemSound(boolean mode, Context context) {
2228

2329
// ANDROID 2.3 - ANDROID 6.0:
2430
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
25-
AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
26-
if (manager != null) {
27-
28-
// ANDROID 6.0+:
29-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
30-
if (mode) {
31-
manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_UNMUTE, 0);
32-
} else {
33-
manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0);
31+
try {
32+
AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
33+
if (manager != null) {
34+
35+
// ANDROID 6.0+:
36+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
37+
if (mode) {
38+
manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_UNMUTE, 0);
39+
} else {
40+
manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0);
41+
}
3442
}
35-
}
3643

37-
// ANDROID 2.3 - ANDROID 5.1:
38-
else {
39-
manager.setStreamMute(AudioManager.STREAM_SYSTEM, mode);
44+
// ANDROID 2.3 - ANDROID 5.1:
45+
else {
46+
manager.setStreamMute(AudioManager.STREAM_SYSTEM, mode);
47+
}
4048
}
49+
} catch (Exception e) {
50+
Log.e(LOG_TAG, "ERROR: An exception occurred while attempting to access the AudioManager: " + e.getLocalizedMessage());
4151
}
4252
}
4353
}
44-
}
54+
}

0 commit comments

Comments
 (0)