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.namelessdev.mpdroid.NotificationService.java

/**
 * We try to get audio focus, but don't really try too hard.
 * We just want the lock screen cover art.
 *///from ww  w. j a  va  2  s  .  co  m
private void tryToGetAudioFocus() {
    if ((!app.getApplicationState().streamingMode || StreamingService.isWoundDown()) && !isAudioFocusedOnThis) {
        Log.d(TAG, "requesting audio focus");
        final int result = mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);

        isAudioFocusedOnThis = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
    }
}

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

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//*from  w w w .  ja  va  2  s  .  com*/
@Override
public void onAudioFocusChange(int 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;
        }
    }
    configMediaPlayerState();
}

From source file:de.qspool.clementineremote.backend.ClementinePlayerConnection.java

/**
 * Register the RemoteControlClient//  w  w w  . ja v  a2s  .c o  m
 */
private void registerRemoteControlClient() {
    // Request AudioFocus, so the widget is shown on the lock-screen
    mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    mAudioManager.registerMediaButtonEventReceiver(mClementineMediaButtonEventReceiver);

    // Create the intent
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mClementineMediaButtonEventReceiver);
    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(App.mApp.getApplicationContext(), 0,
            mediaButtonIntent, 0);
    // Create the client
    mRcClient = new RemoteControlClient(mediaPendingIntent);
    if (App.mClementine.getState() == Clementine.State.PLAY) {
        mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    } else {
        mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
    }
    mRcClient.setTransportControlFlags(
            RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                    | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE);
    mAudioManager.registerRemoteControlClient(mRcClient);
}

From source file:com.adityarathi.muo.services.AudioPlaybackService.java

/**
 * Requests AudioFocus from the OS.// w ww  . j  a va  2 s  .co m
 * 
 * @return True if AudioFocus was gained. False, otherwise.
 */
private boolean requestAudioFocus() {
    int result = mAudioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        //Stop the service.
        mService.stopSelf();
        Toast.makeText(mContext, R.string.close_other_audio_apps, Toast.LENGTH_LONG).show();
        return false;
    } else {
        return true;
    }

}

From source file:com.yohpapa.research.simplemusicplayer.PlaybackService.java

private boolean requestAudioFocus() {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    int result = manager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}

From source file:com.android.onemedia.playback.LocalRenderer.java

private void requestAudioFocus() {
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    mHasAudioFocus = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}

From source file:com.owncloud.android.media.MediaService.java

/**
 * Requests the audio focus to the Audio Manager 
 *///from   w w  w  .  j  a v a  2  s . c  o m
private void tryToGetAudioFocus() {
    if (mAudioFocus != AudioFocus.FOCUS && mAudioManager != null
            && (AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.requestAudioFocus(this,
                    AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN))) {
        mAudioFocus = AudioFocus.FOCUS;
    }
}

From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java

@SuppressLint("NewApi")
public void onPrepared(MediaPlayer mediaPlayer) {
    if (LOCAL_LOGV)
        log("onPrepared()", "v");
    if (mCurrentPlayerState.equals(ServiceRadioPlayer.STATE_RESTARTING)
            || mCurrentPlayerState.equals(ServiceRadioPlayer.STATE_COMPLETE)) {
        if (LOCAL_LOGD)
            log("newPlayer ready", "d");
    } else {/*from w  w w  . j  av  a 2 s  . c o m*/
        int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mAudioFocused = true;

            if (LOCAL_LOGV)
                log("mediaPlayer.start()", "v");
            mediaPlayer.start();
            mCurrentPlayerState = ServiceRadioPlayer.STATE_PLAYING;
            if (mInterrupted) {
                if (LOCAL_LOGV)
                    log("set interrupted = false", "v");
            }
            mInterrupted = false;

            //this.startForegroundNotification(getResources().getString(R.string.status_playing), getResources().getString(R.string.stop), true);
            this.updateNotification(getResources().getString(R.string.status_playing),
                    getResources().getString(R.string.stop), true);

            if (LOCAL_LOGV)
                log("start foreground notification: playing", "v");
            //Toast.makeText(this, "Playing", Toast.LENGTH_SHORT).show();
        } else if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            mAudioFocused = false;

            String title = getResources().getString(R.string.error_title);
            String text = getResources().getString(R.string.error_audio_focus);
            log(text, "w");
            this.getErrorNotification(title, text);
        }

    }
}

From source file:com.lybeat.lilyplayer.widget.media.IjkVideoView.java

@TargetApi(Build.VERSION_CODES.M)
private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;//w  w w  .  j a  va 2s.  c  om
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);

    AudioManager am = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    try {
        mMediaPlayer = createPlayer(mSettings.getPlayer());

        // TODO: create SubtitleController in MediaPlayer, but we need
        // a context for the subtitle renderers
        final Context context = getContext();
        // REMOVED: SubtitleController

        // REMOVED: mAudioSession
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            mMediaPlayer.setDataSource(mAppContext, mUri, mHeaders);
        } else {
            mMediaPlayer.setDataSource(mUri.toString());
        }
        bindSurfaceHolder(mMediaPlayer, mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        if (mHudViewHolder != null)
            mHudViewHolder.setMediaPlayer(mMediaPlayer);

        // REMOVED: mPendingSubtitleTracks

        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } finally {
        // REMOVED: mPendingSubtitleTracks.clear();
    }
}

From source file:com.rks.musicx.services.MusicXService.java

private boolean successfullyRetrievedAudioFocus() {
    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    return result == AudioManager.AUDIOFOCUS_GAIN;
}