Skip to content

Commit

Permalink
HXAudioPlayerUtils: Added additional exception catches and marked ena…
Browse files Browse the repository at this point in the history
…bleSystemSounds as deprecated. Added additional null check in pauseSounds(). Updated build and target SDK to 26.
  • Loading branch information
huhx0015 committed Jul 30, 2017
1 parent 4afbb7e commit fea98b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions hxaudio/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion hxaudio/hxaudio.iml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 26 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;

/** -----------------------------------------------------------------------------------------------
* [HXAudioPlayerUtils] CLASS
Expand All @@ -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());
}
}
}

0 comments on commit fea98b0

Please sign in to comment.