public boolean requestAudioFocus(OnAudioFocusChangeListener focusChangeListener, int streamType, int audioFocusGain) { int r; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { r = mAudioManager.requestAudioFocus( new AudioFocusRequest.Builder(audioFocusGain) .setAudioAttributes( new AudioAttributes.Builder() .setLegacyStreamType(streamType) .build()) .setOnAudioFocusChangeListener(focusChangeListener) .build()); } else { //noinspection deprecation r = mAudioManager.requestAudioFocus(focusChangeListener, streamType, audioFocusGain); } return r == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }
public PlayerService() { musicPlayer = new MusicPlayer(); musicPlayer.addListener(new MusicPlayer.Listener() { @Override public void onCurrentTrackChanged() { updateScrobbling(); } @Override public void onStateChanged() { sentStateBroadcast(); if(musicPlayer.getState() != PlayerState.Preparing) updateNotification(); } }); audioFocusListener = new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { handleAudioFocusChange(focusChange); } }; }
@Override public boolean requestAudioFocus(AudioFocusRequestCompat audioFocusRequestCompat) { // Save the focus request. mAudioFocusRequestCompat = audioFocusRequestCompat; // Check for possible problems... if (audioFocusRequestCompat.acceptsDelayedFocusGain()) { final String message = "Cannot request delayed focus on API " + Build.VERSION.SDK_INT; // Make an exception to allow the developer to more easily find this code path. @SuppressWarnings("ThrowableNotThrown") final Throwable stackTrace = new UnsupportedOperationException(message) .fillInStackTrace(); Log.w(TAG, "Cannot request delayed focus", stackTrace); } final OnAudioFocusChangeListener listener = mAudioFocusRequestCompat.getOnAudioFocusChangeListener(); final int streamType = mAudioFocusRequestCompat.getAudioAttributesCompat().getLegacyStreamType(); final int focusGain = mAudioFocusRequestCompat.getFocusGain(); return mAudioManager.requestAudioFocus(listener, streamType, focusGain) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }
/** * To play sound. * * @param context * {@link Context} * * @param soundID * sound id. * * @param soundPool * {@link SoundPool} instance. * */ public void playSound(Context context, int soundID, SoundPool soundPool){ OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){ // Lower the volume } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Raise it back to normal } } }; AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); // Request audio focus for playback int result = audioManager.requestAudioFocus(afChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // Start playback. soundPool.play(soundID, 10, 10, 1, 0,1f ); } }
public AudioPauser(Context context, boolean isMuteStream) { mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mIsMuteStream = isMuteStream; mAfChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { Log.i("onAudioFocusChange" + focusChange); } }; }
public static void requestAudioFocus(final Context context) { if (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)) { 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); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if(pauseFocus) { pauseFocus = false; downloadService.start(); } if(lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if(focusChange == AudioManager.AUDIOFOCUS_LOSS) { Log.i(TAG, "Permanently lost focus"); focusListener = null; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
@Before public void setup() { context = InstrumentationRegistry.getTargetContext(); // Mockito cannot mock this interface, so just use an instance that does nothing when called listener = new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int i) { // Do nothing } }; assertThat("Precondition 1 failed. Context is null.", context != null); assertThat("Precondition 2 failed. Listener is null.", listener != null); }
public void abandonAudioFocus(OnAudioFocusChangeListener focusChangeListener) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mAudioManager.abandonAudioFocusRequest( new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setOnAudioFocusChangeListener(focusChangeListener) .build()); } else { //noinspection deprecation mAudioManager.abandonAudioFocus(focusChangeListener); } }
private AudioFocusRequestCompat(int focusGain, OnAudioFocusChangeListener onAudioFocusChangeListener, Handler focusChangeHandler, AudioAttributesCompat audioFocusRequestCompat, boolean pauseOnDuck, boolean acceptsDelayedFocusGain) { mFocusGain = focusGain; mOnAudioFocusChangeListener = onAudioFocusChangeListener; mFocusChangeHandler = focusChangeHandler; mAudioAttributesCompat = audioFocusRequestCompat; mPauseOnDuck = pauseOnDuck; mAcceptsDelayedFocusGain = acceptsDelayedFocusGain; }
@NonNull public Builder setOnAudioFocusChangeListener(@NonNull OnAudioFocusChangeListener listener, @NonNull Handler handler) { mOnAudioFocusChangeListener = listener; mFocusChangeHandler = handler; return this; }
@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); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if(pauseFocus) { pauseFocus = false; downloadService.start(); } 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); } }
@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; 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); } }
public static void requestAudioFocus(final Context context) { if (!hasFocus) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); hasFocus = true; audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { DownloadService downloadService = (DownloadService) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { SharedPreferences preferences = getPreferences(context); int lossPref = Integer.parseInt(preferences.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)) { 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); } }
@TargetApi(Build.VERSION_CODES.FROYO) private OnAudioFocusChangeListener createOnAudioFocusChangeListener() { return new OnAudioFocusChangeListener() { private boolean mLossTransient = false; private boolean mLossTransientCanDuck = false; @Override public void onAudioFocusChange(int focusChange) { /* * Pause playback during alerts and notifications */ switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: Log.i(TAG, "AUDIOFOCUS_LOSS"); // Stop playback changeAudioFocus(false); stop(); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT"); // Pause playback if (mMediaPlayer.isPlaying()) { mLossTransient = true; mMediaPlayer.pause(); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"); // Lower the volume if (mMediaPlayer.isPlaying()) { mMediaPlayer.setVolume(36); mLossTransientCanDuck = true; } break; case AudioManager.AUDIOFOCUS_GAIN: Log.i(TAG, "AUDIOFOCUS_GAIN: " + mLossTransientCanDuck + ", " + mLossTransient); // Resume playback if (mLossTransientCanDuck) { mMediaPlayer.setVolume(100); mLossTransientCanDuck = false; } if (mLossTransient) { mMediaPlayer.play(); mLossTransient = false; } break; } } }; }
OnAudioFocusChangeListener getOnAudioFocusChangeListener() { return mOnAudioFocusChangeListener; }
@NonNull public Builder setOnAudioFocusChangeListener(@NonNull OnAudioFocusChangeListener listener) { return setOnAudioFocusChangeListener(listener, new Handler(Looper.getMainLooper())); }
public void play() { try { if (mp != null) { if (!isPlaying()) { if (DBG) { Log.d(LCAT,"audio is not playing, starting."); } setVolume(volume); if (DBG) { Log.d(LCAT, "Play: Volume set to " + volume); } OnAudioFocusChangeListener focusChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { switch (focusChange) { case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) : // Lower the volume while ducking. mp.setVolume(0.2f, 0.2f); break; case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) : pause(); break; case (AudioManager.AUDIOFOCUS_LOSS) : stop(); break; case (AudioManager.AUDIOFOCUS_GAIN) : // Return the volume to normal and resume if paused. mp.setVolume(1f, 1f); play(); break; default: // Nothing break; } } }; am.requestAudioFocus(focusChangeListener, (speakerphone) ? AudioManager.STREAM_MUSIC : AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN); if(!speakerphone) { am.setMode(AudioManager.STREAM_VOICE_CALL); am.setSpeakerphoneOn(false); } else { am.setSpeakerphoneOn(true); } mp.start(); setState(STATE_PLAYING); paused = false; //if (remote) { startProgressTimer(); //} } setState(STATE_PLAYING); } } catch (Throwable t) { Log.w(LCAT, "Issue while playing : " , t); reset(); } }
/** * Requests stream alarm audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamAlarmFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_ALARM); }
/** * Requests stream DTMF audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamDtmfFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_DTMF); }
/** * Requests stream notification audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamNotificationFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_NOTIFICATION); }
/** * Requests stream music audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamMusicFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_MUSIC); }
/** * Requests stream ring audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamRingFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_RING); }
/** * Requests stream system audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamSystemFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_SYSTEM); }
/** * Requests stream voice call audio-focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * * @return true if audio focus is granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static boolean requestStreamVoiceCallFocus( final Context context, final OnAudioFocusChangeListener listener) { return requestStreamAudioFocus(context, listener, AudioManager.STREAM_VOICE_CALL); }
/** * Abandons audio focus. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener which will lose audio focus, not null * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ public static void abandonFocus( final Context context, final OnAudioFocusChangeListener listener) { checkNotNull(context, "context cannot be null"); checkNotNull(listener, "listener cannot be null"); final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); am.abandonAudioFocus(listener); }
/** * Requests a particular type of audio focus for the supplied listener. * * @param context * the Context in which {@code listener} is operating, not null * @param listener * the OnAudioFocusChangeListener to receive the audio focus, not null * @param streamType * the type of stream to request (see static fields of AudioManager) * * @return true if focus was granted, false otherwise * * @throws IllegalArgumentException * if {@code context} is null * @throws IllegalArgumentException * if {@code listener} is null */ private static boolean requestStreamAudioFocus( final Context context, final OnAudioFocusChangeListener listener, final int streamType) { checkNotNull(context, "context cannot be null"); checkNotNull(listener, "listener cannot be null"); final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); final int result = am.requestAudioFocus(listener, streamType, AudioManager.AUDIOFOCUS_GAIN); return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; }
/** * Builds an {@link OnAudioFocusChangeListener} to control an * {@link AudioFocusAwarePlayer} in response to audio focus changes. * <p> * This function is intended to be used in conjuction with an {@link AudioFocusRequestCompat} * as follows: * <code> * AudioFocusRequestCompat focusRequest = * new AudioFocusRequestCompat.Builder(AudioManager.AUDIOFOCUS_GAIN) * .setOnAudioFocusChangeListener(audioFocusHelper.getListenerForPlayer(player)) * // etc... * .build(); * </code> * * @param player The player that will respond to audio focus changes. * @return An {@link OnAudioFocusChangeListener} to control the player. */ public OnAudioFocusChangeListener getListenerForPlayer(@NonNull AudioFocusAwarePlayer player) { if (mDefaultChangeListener != null && mDefaultChangeListener.getPlayer().equals(player)) { return mDefaultChangeListener; } mDefaultChangeListener = new DefaultAudioFocusListener(mImpl, player); return mDefaultChangeListener; }