List of usage examples for android.media AudioManager abandonAudioFocus
public int abandonAudioFocus(OnAudioFocusChangeListener l)
From source file:github.daneren2005.dsub.util.Util.java
@TargetApi(8) public static void requestAudioFocus(final Context context) { if (Build.VERSION.SDK_INT >= 8 && focusListener == null) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadService downloadService = (DownloadService) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isRemoteEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { Log.i(TAG, "Temporary loss of focus"); SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(true); }/* w w w . j a v a 2 s . co m*/ } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isRemoteEnabled()) { Log.i(TAG, "Permanently lost focus"); focusListener = null; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
From source file:github.madmarty.madsonic.util.Util.java
@TargetApi(8) public static void requestAudioFocus(final Context context) { if (Build.VERSION.SDK_INT >= 8 && !hasFocus) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); hasFocus = true;//from w w w . j a v a 2s. c o m audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadServiceImpl downloadService = (DownloadServiceImpl) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_AUDIO_FOCUS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) { hasFocus = false; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }