Example usage for android.media AudioManager AUDIOFOCUS_GAIN

List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN

Introduction

In this page you can find the example usage for android.media AudioManager AUDIOFOCUS_GAIN.

Prototype

int AUDIOFOCUS_GAIN

To view the source code for android.media AudioManager AUDIOFOCUS_GAIN.

Click Source Link

Document

Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.

Usage

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

private void preparePlayer() {
    // Request audio focus for playback
    int result = audioManager.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        // Start playback
        String streamUrl = restService.getConnection().getUrl() + "/stream/"
                + player.getTranscodeProfile().getID() + "?offset=" + (int) (player.getOffset() * 0.001);
        Uri contentUri = Uri.parse(streamUrl);

        player.addListener(this);
        player.initialise(this, contentUri, true);

        wakeLock.acquire();// w  w w . j  a  v a 2s  .com
        wifiLock.acquire();

        if (!mediaSession.isActive()) {
            mediaSession.setActive(true);
        }

        // Register receiver for audio manager events
        registerAudioManagerReceiver();
    }
}

From source file:nuclei.media.playback.ExoPlayerPlayback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//* w ww .j  av  a 2  s  .c  om*/
@Override
public void onAudioFocusChange(int focusChange) {
    if (LOG.isLoggable(Log.INFO))
        LOG.d("onAudioFocusChange. focusChange=" + focusChange);
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // We have gained focus:
        mAudioFocus = AUDIO_FOCUSED;

    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        // We have lost focus. If we can duck (low playback volume), we can keep playing.
        // Otherwise, we need to pause the playback.
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        // If we are playing, we need to reset media player by calling configMediaPlayerState
        // with mAudioFocus properly set.
        if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
            // If we don't have audio focus and can't duck, we save the information that
            // we were playing, so that we can resume playback once we get the focus back.
            mPlayOnFocusGain = true;
        }
    } else {
        LOG.e("onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
    }
    configMediaPlayerState(false, false);
}

From source file:com.sourceauditor.sahomemonitor.MainActivity.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        if (paused()) {
            play();//from   w  ww. j a v  a 2s .co  m
        } else {
            audioPlayer.setVolume(1.0f, 1.0f);
        }
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        // Lost focus for an unbounded amount of time: pause playback
        pause();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // Lost focus for a short time, but it's ok to keep playing
        // at an attenuated level
        if (!paused()) {
            audioPlayer.setVolume(0.1f, 0.1f);

        }
        break;
    }
}

From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java

@Override
public void onAudioFocusChange(int focusState) {
    //Invoked when the audio focus of the system is updated.
    switch (focusState) {
    case AudioManager.AUDIOFOCUS_GAIN:
        // resume playback
        if (mediaPlayer == null)
            initMediaPlayer();/*  w ww  . j a  v  a 2  s. co  m*/
        else if (!mediaPlayer.isPlaying())
            mediaPlayer.start();
        mediaPlayer.setVolume(1.0f, 1.0f);
        break;
    case AudioManager.AUDIOFOCUS_LOSS:
        // Lost focus for an unbounded amount of time: stop playback and release media player
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // Lost focus for a short time, but we have to stop
        // playback. We don't release the media player because playback
        // is likely to resume
        if (mediaPlayer.isPlaying())
            mediaPlayer.pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // Lost focus for a short time, but it's ok to keep playing
        // at an attenuated level
        if (mediaPlayer.isPlaying())
            mediaPlayer.setVolume(0.1f, 0.1f);
        break;
    }
}

From source file:com.google.android.car.kitchensink.audio.AudioTestFragment.java

private void handleRadioStart() {
    if (mCarAudioManager == null) {
        return;/*  ww w  . j a va  2s . com*/
    }
    if (DBG) {
        Log.i(TAG, "Radio start");
    }
    try {
        mCarAudioManager.requestAudioFocus(mRadioFocusListener, mRadioAudioAttrib, AudioManager.AUDIOFOCUS_GAIN,
                0);
    } catch (CarNotConnectedException e) {
        Log.e(TAG, "failed", e);
    }
}

From source file:com.andryr.musicplayer.PlaybackService.java

public void play() {
    int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mMediaPlayer.start();/*from   w ww  .j  av  a 2s  .  c  o m*/
        mIsPlaying = true;
        mIsPaused = false;
        notifyChange(PLAYSTATE_CHANGED);
    }

}

From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java

private boolean requestAudioFocus() {
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        //Focus gained
        return true;
    }/* ww w. j ava  2  s  .  c om*/
    //Could not gain focus
    return false;
}

From source file:com.fastbootmobile.encore.service.PlaybackService.java

/**
 * Request the audio focus and registers the remote media controller
 *//*from www  .  jav a  2 s  .c om*/
private synchronized void requestAudioFocus() {
    if (!mHasAudioFocus) {
        final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        // Request audio focus for music playback
        int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            am.registerMediaButtonEventReceiver(RemoteControlReceiver.getComponentName(this));

            // Notify the remote metadata that we're getting active
            mRemoteMetadata.setActive(true);

            // Register AUDIO_BECOMING_NOISY to stop playback when earbuds are pulled
            registerReceiver(mAudioNoisyReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

            // Add a WakeLock to avoid CPU going to sleep while music is playing
            mWakeLock.acquire();

            mHasAudioFocus = true;
        } else {
            Log.e(TAG, "Audio focus request denied: " + result);
        }
    }
}

From source file:com.smithdtyler.prettygoodmusicplayer.MusicPlaybackService.java

private synchronized void play() {
    pauseTime = Long.MAX_VALUE;/*from w w w.  j a  v a  2 s  .  com*/
    if (mp.isPlaying()) {
        // do nothing
    } else {
        // Request audio focus for playback
        int result = am.requestAudioFocus(MusicPlaybackService.this.audioFocusListener,
                // Use the music stream.
                AudioManager.STREAM_MUSIC,
                // Request permanent focus.
                AudioManager.AUDIOFOCUS_GAIN);
        Log.d(TAG, "requestAudioFocus result = " + result);
        Log.i(TAG, "About to play " + songFile);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Log.d(TAG, "We got audio focus!");
            mp.start();
            updateNotification();
            wakeLock.acquire();
        } else {
            Log.e(TAG, "Unable to get audio focus");
        }
    }
}

From source file:androidx.media.widget.VideoView2.java

/**
 * Sets which type of audio focus will be requested during the playback, or configures playback
 * to not request audio focus. Valid values for focus requests are
 * {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT},
 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and
 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use
 * {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be
 * requested when playback starts. You can for instance use this when playing a silent animation
 * through this class, and you don't want to affect other audio applications playing in the
 * background./*  ww w .  j  a va  2 s . c  o m*/
 *
 * @param focusGain the type of audio focus gain that will be requested, or
 *                  {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during
 *                  playback.
 */
public void setAudioFocusRequest(int focusGain) {
    if (focusGain != AudioManager.AUDIOFOCUS_NONE && focusGain != AudioManager.AUDIOFOCUS_GAIN
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) {
        throw new IllegalArgumentException("Illegal audio focus type " + focusGain);
    }
    mAudioFocusType = focusGain;
}