Example usage for android.media AudioManager AUDIOFOCUS_LOSS

List of usage examples for android.media AudioManager AUDIOFOCUS_LOSS

Introduction

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

Prototype

int AUDIOFOCUS_LOSS

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

Click Source Link

Document

Used to indicate a loss of audio focus of unknown duration.

Usage

From source file:org.y20k.transistor.PlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    // gain of audio focus of unknown duration
    case AudioManager.AUDIOFOCUS_GAIN:
        if (mPlayback) {
            if (mMediaPlayer == null) {
                initializeMediaPlayer();
            } else if (!mMediaPlayer.isPlaying()) {
                mMediaPlayer.start();/* w  w w  .jav a  2s  . c om*/
            }
            mMediaPlayer.setVolume(1.0f, 1.0f);
        }
        break;
    // loss of audio focus of unknown duration
    case AudioManager.AUDIOFOCUS_LOSS:
        stopPlayback();
        break;
    // transient loss of audio focus
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        if (!mPlayback && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            stopPlayback();
        } else if (mPlayback && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
        }
        break;
    // temporary external request of audio focus
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.setVolume(0.1f, 0.1f);
        }
        break;
    }
}

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

public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
        pause();/*from  ww w. j a v  a2 s  .  c o  m*/
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // Need to sync this so that we don't react to our own events
        if (!isPlaying()) {
            // unpause();
        }
    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
        abandonAudioFocus();
        pause();
    }
}

From source file:bala.padio.Player.java

@Override
public void onAudioFocusChange(int focusChange) {
    Log.e(TAG, "onAudioFocusChange: " + focusChange);
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
        // resume playback
        if (mediaPlayer == null || !mediaPlayer.isPlaying()) {
            Play(playerUrl);//from w  ww .ja  va  2 s. c om
        }
        mediaPlayer.setVolume(1.0f, 1.0f);
        break;

    case AudioManager.AUDIOFOCUS_LOSS:
        // Lost focus for an unbounded amount of time: stop playback and release media player
        Stop();
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        // Lost focus for a short time, but we have to stop
        // playback. We don't release the media player because playback
        // is likely to resume
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.reset();
        }
        break;

    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        // Lost focus for a short time, but it's ok to keep playing
        // at an attenuated level
        if (mediaPlayer.isPlaying())
            mediaPlayer.setVolume(0.1f, 0.1f);
        break;
    }
}

From source file:com.ironsmile.cordova.mediaevents.MediaEventListener.java

/**
 * Updates the JavaScript side with new audio focus event
 *
 * @param focusEvent the received event//  w  w  w.ja  v a2 s .c  o  m
 * @return
 */
private void sendFocusEvent(int focusEvent) {
    JSONObject obj = new JSONObject();
    try {
        switch (focusEvent) {
        case AudioManager.AUDIOFOCUS_GAIN:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
            obj.put("type", "audiofocusgain");
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            obj.put("type", "audiofocusloss");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            obj.put("type", "audiofocuslosstransient");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            obj.put("type", "audiofocuslosstransientcanduck");
            break;
        }

    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    sendUpdate(obj, true);
}

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();//ww w. j  a  va2 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.mishiranu.dashchan.content.service.AudioPlayerService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS: {
        pause(true);/*from  w  ww  . ja  v  a2  s. co  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.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 ww .jav a 2  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.google.fpl.gim.examplegame.MainService.java

@Override
public void onCreate() {
    // The service is being created.
    Utils.logDebug(TAG, "onCreate");
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_1);
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_2);
    intentFilter.addAction(CHOICE_NOTIFICATION_ACTION_3);
    registerReceiver(mReceiver, intentFilter);

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // Determines the behavior for handling Audio Focus surrender.
    mAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override/*from ww w  . j  ava2  s  .  co m*/
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                    || focusChange == AudioManager.AUDIOFOCUS_LOSS) {

                if (mTextToSpeech.isSpeaking()) {
                    mTextToSpeech.setOnUtteranceProgressListener(null);
                    mTextToSpeech.stop();
                }

                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }

                // Abandon Audio Focus, if it's requested elsewhere.
                mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);

                // Restart the current moment if AudioFocus was lost. Since AudioFocus is only
                // requested away from this application if this application was using it,
                // only Moments that play sound will restart in this way.
                if (mMission != null) {
                    mMission.restartMoment();
                }
            }
        }
    };

    // Asynchronously prepares the TextToSpeech.
    mTextToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                // Check if language is available.
                switch (mTextToSpeech.isLanguageAvailable(DEFAULT_TEXT_TO_SPEECH_LOCALE)) {
                case TextToSpeech.LANG_AVAILABLE:
                case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                    Utils.logDebug(TAG, "TTS locale supported.");
                    mTextToSpeech.setLanguage(DEFAULT_TEXT_TO_SPEECH_LOCALE);
                    mIsTextToSpeechReady = true;
                    break;
                case TextToSpeech.LANG_MISSING_DATA:
                    Utils.logDebug(TAG, "TTS missing data, ask for install.");
                    Intent installIntent = new Intent();
                    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installIntent);
                    break;
                default:
                    Utils.logDebug(TAG, "TTS local not supported.");
                    break;
                }
            }
        }
    });

    mMediaPlayer = new MediaPlayer();
}

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

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
        stop();//ww  w.ja v  a 2s  .  c o  m
        break;
    case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
        stopSelf();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        lowerVolume();
        break;
    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
    case AudioManager.AUDIOFOCUS_GAIN:
        restoreVolume();
        break;
    }
}

From source file:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        if (mRingtone.isPlaying())
            mRingtone.stop();//from   ww  w.  j  a va 2 s . c  om
        break;
    }
}