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.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 www . ja  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 {//w w  w .  j  av  a  2s.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.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();//from w ww . j a  v a 2  s.co m
        wifiLock.acquire();

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

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

From source file:com.hotstar.player.adplayer.player.PlayerFragment.java

private void requestAudioFocus() {
    AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Request audio focus for playback
    int result = am.requestAudioFocus(null,
            // Use the music stream.
            AudioManager.STREAM_MUSIC,//  w ww  . j av  a2  s. c  om
            // Request permanent focus.
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        AdVideoApplication.logger.i(LOG_TAG + "#requestAudioFocus()", "Gained audio focus.");
    }
}

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();/*  w w w  . j ava 2  s .  co  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;
    }// w  ww.  j  a  v a2 s .com
    //Could not gain focus
    return false;
}

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

private boolean removeAudioFocus() {
    return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == audioManager.abandonAudioFocus(this);
}

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

/**
 * Request the audio focus and registers the remote media controller
 *///from   w w  w . j  a  v a 2s  . c o m
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);
        }
    }
}