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.reallynourl.nourl.fmpfoldermusicplayer.backend.MediaService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        if (mMediaPlayer != null) {
            mMediaPlayer.setVolume(1.0f, 1.0f);
        }/*from  w  w w  . ja  v a2  s .  c  o  m*/
        break;
    case AudioManager.AUDIOFOCUS_LOSS:
        abandonAudioFocus();
        if (mMediaPlayer != null) {
            releasePlayer();
        }
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.setVolume(0.15f, 0.15f);
        }
        break;
    }
}

From source file:com.mylovemhz.simplay.MusicService.java

private void cueTrack() throws IOException, IndexOutOfBoundsException {
    Log.d(TAG_MUSIC_SERVICE, "Cue Track...");

    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result != AudioManager.AUDIOFOCUS_GAIN) {
        return; //Failed to gain audio focus
    }//  ww  w . j a  v a 2 s  .c om

    if (getCurrentState() == State.STOPPED) {
        mediaPlayer.reset();
        currentState = State.IDLE;

    }
    if (getCurrentState() == State.IDLE) {
        if (trackQueue.size() == 0) {
            return; //nothing to play
        }
        if (hasPermission(Manifest.permission.WAKE_LOCK)) {
            if (!wifiLock.isHeld()) {
                wifiLock.acquire();
            }
            Track track = trackQueue.get(currentTrackIndex);
            mediaPlayer.setDataSource(track.getUrl());
            currentState = State.INITIALIZED;
            mediaPlayer.prepareAsync();
            currentState = State.PREPARING;
            if (callbacks != null)
                callbacks.onLoading();
        } else {
            Log.e(TAG_MUSIC_SERVICE, "need permission " + Manifest.permission.WAKE_LOCK);
            if (callbacks != null) {
                callbacks.onPermissionRequired(REQUEST_PERMISSION_WAKE_LOCK, Manifest.permission.WAKE_LOCK,
                        RATIONALE_WAKE_LOCK);
            }
        }
    }
}

From source file:com.mishiranu.dashchan.content.service.AudioPlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS: {
        pause(true);/*from w  w w  .  jav  a  2  s.c  o  m*/
        break;
    }
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: {
        boolean playing = mediaPlayer.isPlaying();
        pause(false);
        if (playing) {
            pausedByTransientLossOfFocus = true;
        }
        break;
    }
    case AudioManager.AUDIOFOCUS_GAIN: {
        if (pausedByTransientLossOfFocus) {
            play(false);
        }
        break;
    }
    }
}

From source file:com.example.android.miwok.activity.ColorsActivity.java

private AudioManager.OnAudioFocusChangeListener getAudioFocusChangeCallback() {

    return new AudioManager.OnAudioFocusChangeListener() {
        Handler mHandler = new Handler();

        public void onAudioFocusChange(int focusChange) {

            // The AUDIOFOCUS_LOSS case means we've lost audio focus permanently
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                // Stop playback and clean up resources
                releaseMediaPlayer();/*www .jav a 2  s.  c o m*/
            }
            // The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a
            // short amount of time.
            else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
                // Pause playback immediately
                mMediaPlayer.pause();
                // play the word from the beginning when we resume playback.
                mMediaPlayer.seekTo(0);
            }
            //The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that
            // our app is allowed to continue playing sound but at a lower volume.
            else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                // Lower the volume, keep playing
                mAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
            }
            // The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
            else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                // Raise volume to normal, restart playback if necessary
                mMediaPlayer.start();
                mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0);

            }
        }
    };
}

From source file:com.classiqo.nativeandroid_32bitz.playback.LocalPlayback.java

private void tryToGetAudioFocus() {
    LogHelper.d(TAG, "tryToGetAudioFocus");
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocus = AUDIO_FOCUSED;/*from   w w  w. j  ava2 s  . c o  m*/
    } else {
        mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

From source file:com.lithiumli.fiction.PlaybackService.java

private boolean acquireAudioFocus() {
    int result = getAudioManager().requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return true;
    }/* w w  w .  j av a2 s  .c om*/
    return false;
}

From source file:com.allthatseries.RNAudioPlayer.Playback.java

/**
 * Try to get the system audio focus./*from ww w  . j  av a  2s .c  o  m*/
 */
private void tryToGetAudioFocus() {
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocus = AUDIO_FOCUSED;
    } else {
        mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

From source file:com.example.android.supportv4.media.Playback.java

/**
 * Try to get the system audio focus.//from  www . ja  v a2 s  .co m
 */
private void tryToGetAudioFocus() {
    Log.d(TAG, "tryToGetAudioFocus");
    if (mAudioFocus != AUDIO_FOCUSED) {
        int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mAudioFocus = AUDIO_FOCUSED;
        }
    }
}

From source file:com.mishiranu.dashchan.content.service.AudioPlayerService.java

private boolean play(boolean resetFocus) {
    if (resetFocus && audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return false;
    }//from ww w  .  jav a  2 s. com
    mediaPlayer.start();
    wakeLock.acquire();
    return true;
}

From source file:com.appdevper.mediaplayer.app.LocalPlayback.java

/**
 * Try to get the system audio focus.//from  w  w  w.j a v a2  s .  co  m
 */
private void tryToGetAudioFocus() {
    LogHelper.d(TAG, "tryToGetAudioFocus");
    if (mAudioFocus != AUDIO_FOCUSED) {
        int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mAudioFocus = AUDIO_FOCUSED;
        }
    }
}