From fea98b06f3ae8cff5b1d6b6be459bec08890f66a Mon Sep 17 00:00:00 2001 From: huhx0015 Date: Sat, 29 Jul 2017 18:51:13 -0700 Subject: [PATCH] HXAudioPlayerUtils: Added additional exception catches and marked enableSystemSounds as deprecated. Added additional null check in pauseSounds(). Updated build and target SDK to 26. --- hxaudio/build.gradle | 10 ++-- hxaudio/hxaudio.iml | 2 +- .../huhx0015/hxaudio/audio/HXMusicEngine.java | 2 +- .../hxaudio/utils/HXAudioPlayerUtils.java | 48 +++++++++---------- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/hxaudio/build.gradle b/hxaudio/build.gradle index 0a96fbc..e4a7e17 100644 --- a/hxaudio/build.gradle +++ b/hxaudio/build.gradle @@ -1,14 +1,14 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion 26 + buildToolsVersion "26.0.1" defaultConfig { minSdkVersion 9 - targetSdkVersion 25 - versionCode 11 - versionName "3.2.0" + targetSdkVersion 26 + versionCode 15 + versionName "3.2.3" } buildTypes { release { diff --git a/hxaudio/hxaudio.iml b/hxaudio/hxaudio.iml index a4b3776..1c25b44 100644 --- a/hxaudio/hxaudio.iml +++ b/hxaudio/hxaudio.iml @@ -93,7 +93,7 @@ - + \ No newline at end of file diff --git a/hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusicEngine.java b/hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusicEngine.java index 40a7cf3..515447d 100644 --- a/hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusicEngine.java +++ b/hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusicEngine.java @@ -265,7 +265,7 @@ int pauseMusic() { musicPosition = currentPlayer.getCurrentPosition(); // Retrieves the current music position. // Pauses the music only if there is a music is currently playing. - if (currentPlayer.isPlaying()) { + if (currentPlayer != null && currentPlayer.isPlaying()) { removeNextMediaPlayer(); // Prevents nextPlayer from starting after currentPlayer has completed playback. currentPlayer.pause(); // Pauses the music. diff --git a/hxaudio/src/main/java/com/huhx0015/hxaudio/utils/HXAudioPlayerUtils.java b/hxaudio/src/main/java/com/huhx0015/hxaudio/utils/HXAudioPlayerUtils.java index a68370c..934cc9e 100644 --- a/hxaudio/src/main/java/com/huhx0015/hxaudio/utils/HXAudioPlayerUtils.java +++ b/hxaudio/src/main/java/com/huhx0015/hxaudio/utils/HXAudioPlayerUtils.java @@ -3,7 +3,6 @@ import android.content.Context; import android.media.AudioManager; import android.os.Build; -import android.util.Log; /** ----------------------------------------------------------------------------------------------- * [HXAudioPlayerUtils] CLASS @@ -23,32 +22,33 @@ public class HXAudioPlayerUtils { // enableSystemSound(): Enables or disables the device's system sound effects. This is most // commonly used if physical button's sound effects need to be enabled/disabled. - // NOTE: Android 7.0 and above requires "android.permission.ACCESS_NOTIFICATION_POLICY". + // NOTE 1: Android 7.0 and above requires "android.permission.ACCESS_NOTIFICATION_POLICY". + // NOTE 2: This method has been marked as deprecated and will be removed in the next major + // release of HXAudioPlayer. + @Deprecated public static void enableSystemSound(boolean mode, Context context) { - - // ANDROID 2.3 - ANDROID 6.0: - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { - try { - AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - if (manager != null) { - - // ANDROID 6.0+: - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (mode) { - manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_UNMUTE, 0); - } else { - manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0); - } - } - - // ANDROID 2.3 - ANDROID 5.1: - else { - manager.setStreamMute(AudioManager.STREAM_SYSTEM, mode); - } + try { + AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + + // ANDROID 6.0+: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (mode) { + manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_UNMUTE, 0); + } else { + manager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0); } - } catch (Exception e) { - Log.e(LOG_TAG, "ERROR: An exception occurred while attempting to access the AudioManager: " + e.getLocalizedMessage()); } + + // ANDROID 2.3 - ANDROID 5.1: + else { + manager.setStreamMute(AudioManager.STREAM_SYSTEM, mode); + } + } catch (NullPointerException e) { + HXLog.e(LOG_TAG, "ERROR: An null pointer exception occurred while attempting to access the AudioManager: " + e.getLocalizedMessage()); + } catch (SecurityException e) { + HXLog.e(LOG_TAG, "ERROR: An security exception occurred while attempting to access the AudioManager: " + e.getLocalizedMessage()); + } catch (Exception e) { + HXLog.e(LOG_TAG, "ERROR: An exception occurred while attempting to access the AudioManager: " + e.getLocalizedMessage()); } } } \ No newline at end of file