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.example.chu.myvideodemo.MusicService.java

public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D)/*w w  w .j a  v  a2s. c  om*/
        Log.d(TAG, "Starting playback: audio focus request status = " + status);

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
    sendBroadcast(intent);

    mAudioManager.registerMediaButtonEventReceiver(
            new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        mSession.setActive(true);

    if (createNewNextTrack) {
        setNextTrack();
    } else {
        setNextTrack(mNextPlayPos);
    }

    if (mPlayer.isInitialized()) {
        final long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();
        mPlayerHandler.removeMessages(FADEDOWN);
        mPlayerHandler.sendEmptyMessage(FADEUP);

        setIsSupposedToBePlaying(true, true);

        cancelShutdown();
        //updateNotification();
        notifyChange(META_CHANGED);
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}

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

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        // You have gained the audio focus.
        mNativeHub.setDucking(false);//  w  w  w  .ja v a2  s  . com
        if (mState != STATE_PLAYING && mPausedByFocusLoss) {
            playImpl();
            mPausedByFocusLoss = false;
        }
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        // You have lost the audio focus for a presumably long time. You must stop all audio
        // playback.
        pauseImpl();
        mPausedByFocusLoss = true;
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // You have temporarily lost audio focus, but should receive it back shortly. You
        // must stop all audio playback, but you can keep your resources because you will
        // probably get focus back shortly.
        pauseImpl();
        mPausedByFocusLoss = true;
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // You have temporarily lost audio focus, but you are allowed to continue to play
        // audio quietly (at a low volume) instead of killing audio completely.
        mNativeHub.setDucking(true);
        break;
    }
}

From source file:com.ferdi2005.secondgram.voip.VoIPService.java

private void configureDeviceForCall() {
    needPlayEndSound = true;/*from  w  w  w . j  a  v a2 s. c  om*/
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    am.setMode(AudioManager.MODE_IN_COMMUNICATION);
    am.setSpeakerphoneOn(false);
    am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);

    SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    try {
        if (proximity != null) {
            proximityWakelock = ((PowerManager) getSystemService(Context.POWER_SERVICE))
                    .newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
            sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
        }
    } catch (Exception x) {
        FileLog.e("Error initializing proximity sensor", x);
    }
}

From source file:com.ferdi2005.secondgram.voip.VoIPService.java

@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        haveAudioFocus = true;//  w ww.  ja  v a2  s  . c om
    } else {
        haveAudioFocus = false;
    }
}

From source file:com.naman14.timber.musicplayer.MusicService.java

public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D) {//from   ww w.j ava 2 s.c o  m
        Log.d(TAG, "Starting playback: audio focus request status = " + status);
    }

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
    sendBroadcast(intent);

    mAudioManager.registerMediaButtonEventReceiver(
            new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        mSession.setActive(true);

    if (createNewNextTrack) {
        setNextTrack();
    } else {
        setNextTrack(mNextPlayPos);
    }

    if (mPlayer.isInitialized()) {
        final long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();
        mPlayerHandler.removeMessages(FADEDOWN);
        mPlayerHandler.sendEmptyMessage(FADEUP);

        setIsSupposedToBePlaying(true, true);

        cancelShutdown();
        updateNotification();
        notifyChange(META_CHANGED);
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}

From source file:com.android.tv.MainActivity.java

private void setVolumeByAudioFocusStatus(TunableTvView tvView) {
    SoftPreconditions.checkState(tvView == mTvView || tvView == mPipView);
    if (tvView.isPlaying()) {
        switch (mAudioFocusStatus) {
        case AudioManager.AUDIOFOCUS_GAIN:
            tvView.setStreamVolume(AUDIO_MAX_VOLUME);
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            tvView.setStreamVolume(AUDIO_MIN_VOLUME);
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            tvView.setStreamVolume(AUDIO_DUCKING_VOLUME);
            break;
        }//w  w w  .  j av  a2 s . c o  m
    }
    if (tvView == mTvView) {
        if (mPipView != null && mPipView.isPlaying()) {
            mPipView.setStreamVolume(AUDIO_MIN_VOLUME);
        }
    } else { // tvView == mPipView
        if (mTvView != null && mTvView.isPlaying()) {
            mTvView.setStreamVolume(AUDIO_MIN_VOLUME);
        }
    }
}

From source file:com.bluros.music.MusicService.java

public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D)/* www  .ja va2s. co m*/
        Log.d(TAG, "Starting playback: audio focus request status = " + status);

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
    sendBroadcast(intent);

    mAudioManager.registerMediaButtonEventReceiver(
            new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        mSession.setActive(true);

    if (createNewNextTrack) {
        setNextTrack();
    } else {
        setNextTrack(mNextPlayPos);
    }

    if (mPlayer.isInitialized()) {
        final long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();
        mPlayerHandler.removeMessages(FADEDOWN);
        mPlayerHandler.sendEmptyMessage(FADEUP);

        setIsSupposedToBePlaying(true, true);

        cancelShutdown();
        updateNotification();
        notifyChange(META_CHANGED);
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}

From source file:com.koma.music.service.MusicService.java

/**
 * Resumes or starts playback.//w w w.  java  2s. c o m
 *
 * @param createNewNextTrack True if you want to figure out the next track, false
 *                           if you want to re-use the existing next track (used for going back)
 */
public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    LogUtils.d(TAG, "Starting playback: audio focus request status = " + status);

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
    sendBroadcast(intent);

    mSession.setActive(true);

    if (createNewNextTrack) {
        setNextTrack();
    } else {
        setNextTrack(mNextPlayPos);
    }

    if (mPlayer.isInitialized()) {
        final long duration = mPlayer.duration();
        if (mRepeatMode != MusicServiceConstants.REPEAT_CURRENT && duration > 2000
                && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();
        mPlayerHandler.removeMessages(MusicServiceConstants.FADEDOWN);
        mPlayerHandler.sendEmptyMessage(MusicServiceConstants.FADEUP);

        setIsSupposedToBePlaying(true, true);

        cancelShutdown();
        updateNotification();
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(MusicServiceConstants.SHUFFLE_AUTO);
    }
}

From source file:com.andrew.apollo.MusicPlaybackService.java

/**
 * Resumes or starts playback./*www  . ja va 2s.  co m*/
 */
public void play() {
    if (mAudioManager == null) {
        return;
    }

    stopSimplePlayer();

    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D)
        LOG.info("Starting playback: audio focus request status = " + status);

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    try {
        mAudioManager.registerMediaButtonEventReceiver(
                new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
    } catch (SecurityException e) {
        e.printStackTrace();
        // see explanation in initService
    }

    if (mPlayer != null && mPlayer.isInitialized()) {
        setNextTrack();

        if (mShuffleEnabled && (mHistory.empty() || mHistory.peek() != mPlayPos)) {
            mHistory.push(mPlayPos);
        }

        final long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();

        if (mPlayerHandler != null) {
            mPlayerHandler.removeMessages(FADE_DOWN);
            mPlayerHandler.sendEmptyMessage(FADE_UP);
        }

        if (!mIsSupposedToBePlaying) {
            mIsSupposedToBePlaying = true;
            notifyChange(PLAYSTATE_CHANGED);
        }
        cancelShutdown();
        updateNotification();
    }
}

From source file:mp.teardrop.PlaybackService.java

public void onAudioFocusChange(int type) {
    Log.d("OrchidMP", "audio focus change: " + type);
    switch (type) {
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        mDuckedLoss = (mState & FLAG_PLAYING) != 0;
        unsetFlag(FLAG_PLAYING);//from   ww w.j  av  a  2s  .  c om
        break;
    case AudioManager.AUDIOFOCUS_LOSS:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        mDuckedLoss = false;
        mForceNotificationVisible = true;
        unsetFlag(FLAG_PLAYING);
        break;
    case AudioManager.AUDIOFOCUS_GAIN:
        if (mDuckedLoss) {
            mDuckedLoss = false;
            setFlag(FLAG_PLAYING);
        }
        break;
    }
}