Example usage for android.media AudioManager AUDIOFOCUS_REQUEST_GRANTED

List of usage examples for android.media AudioManager AUDIOFOCUS_REQUEST_GRANTED

Introduction

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

Prototype

int AUDIOFOCUS_REQUEST_GRANTED

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

Click Source Link

Document

A successful focus change request.

Usage

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) {/*w  w  w .  ja  v  a2s  .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.bluros.music.MusicService.java

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

    if (D)/*from w  w  w . j  ava  2  s. com*/
        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.ja  v  a  2  s .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./* w  w  w .  ja  v  a  2s .  c  o  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:com.av.remusic.service.MediaService.java

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

    if (D)//from www .  j a  v  a2  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.isTrackPrepared()) {
        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);
}

From source file:com.cyanogenmod.eleven.MusicPlaybackService.java

/**
 * Resumes or starts playback.//from   w  w w . j a va2s. c  om
 *
 * @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);

    if (D)
        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()));
    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();
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}