Example usage for android.media AudioManager AUDIOFOCUS_GAIN_TRANSIENT

List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN_TRANSIENT

Introduction

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

Prototype

int AUDIOFOCUS_GAIN_TRANSIENT

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

Click Source Link

Document

Used to indicate a temporary gain or request of audio focus, anticipated to last a short amount of time.

Usage

From source file:org.videolan.vlc.PlaybackService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!AndroidUtil.isFroyoOrLater()) // NOP if not supported
        return;//  ww  w  . ja  v  a2s. c o  m

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                if (!hasCurrentMedia())
                    return;
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (MediaPlayer().isPlaying())
                        MediaPlayer().pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    /*
                     * Lower the volume to 36% to "duck" when an alert or something
                     * needs to be played.
                     */
                    MediaPlayer().setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    MediaPlayer().setVolume(100);
                    break;
                }
            }
        };
    }

    AudioManager am = (AudioManager) VLCApplication.getAppContext().getSystemService(AUDIO_SERVICE);
    if (gain)
        am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    else
        am.abandonAudioFocus(audioFocusListener);

}

From source file:com.google.fpl.gim.examplegame.MainService.java

/**
 * Obtain audio focus for the application. This also checks if we are currently playing any
 * other audio clips, so it checks for "audio focus" within the app.
 * @return True if audio focus is obtained. False otherwise.
 *//* www  . j a v  a2  s  .com*/
public boolean obtainAudioFocus() {
    if (mMediaPlayer.isPlaying() || mTextToSpeech.isSpeaking()) {
        return false;
    }

    int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    return (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
}

From source file:com.google.android.car.kitchensink.audio.AudioTestFragment.java

private void handleVrStart() {
    if (mAppFocusManager == null) {
        return;/*from   w  ww  .  j  av  a2s.co m*/
    }
    if (mCarAudioManager == null) {
        return;
    }
    if (DBG) {
        Log.i(TAG, "VR start");
    }
    try {
        mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, mOwnershipCallbacks);
        mCarAudioManager.requestAudioFocus(mVrFocusListener, mVrAudioAttrib,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, 0);
    } catch (CarNotConnectedException e) {
        Log.e(TAG, "Failed to set active focus", e);
    }
}

From source file:com.evandroid.musica.MainLyricActivity.java

@SuppressLint("InlinedApi")
public void resync(MenuItem item) {
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null,// www.jav a2s  .c o m
            // Use the music stream.
            AudioManager.STREAM_SYSTEM,
            // Request permanent focus.
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    am.abandonAudioFocus(null);
}

From source file:androidx.media.widget.VideoView2.java

/**
 * Sets which type of audio focus will be requested during the playback, or configures playback
 * to not request audio focus. Valid values for focus requests are
 * {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT},
 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and
 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use
 * {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be
 * requested when playback starts. You can for instance use this when playing a silent animation
 * through this class, and you don't want to affect other audio applications playing in the
 * background./*from  w w w.  ja v  a2 s. c  o m*/
 *
 * @param focusGain the type of audio focus gain that will be requested, or
 *                  {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during
 *                  playback.
 */
public void setAudioFocusRequest(int focusGain) {
    if (focusGain != AudioManager.AUDIOFOCUS_NONE && focusGain != AudioManager.AUDIOFOCUS_GAIN
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) {
        throw new IllegalArgumentException("Illegal audio focus type " + focusGain);
    }
    mAudioFocusType = focusGain;
}

From source file:com.example.mydemos.view.RingtonePickerActivity.java

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    Log.e("lys", "onItemClick position ==" + position + "id" + id);
    if (id == -1) {
        if (mHasSilentItem) {

            if ((mHasDefaultItem && (position == 1)) || !mHasDefaultItem) {
                Log.e("lys", "onItemClick silentItemChecked == true");
                //toneCur.moveToPosition(position);
                mSelectedId = SILENT_ID;
                mSelectedUri = null;//from ww w. j ava2s  .c om
                silentItemChecked = true;
                defaultItemChecked = false;
                stopMediaPlayer();
                listView.invalidateViews();
                return;
            } else if (mHasDefaultItem && (position == 0)) {
                Log.e("lys", "onItemClick defaultItemChecked == true");
                //toneCur.moveToPosition(position);
                mSelectedId = DEFAULT_ID;
                mSelectedUri = mUriForDefaultItem;
                defaultItemChecked = true;
                silentItemChecked = false;
            }
        } else {
            if (mHasDefaultItem) {
                Log.e("lys", "onItemClick defaultItemChecked == true");
                //toneCur.moveToPosition(position);
                mSelectedId = DEFAULT_ID;
                mSelectedUri = mUriForDefaultItem;
                defaultItemChecked = true;
                silentItemChecked = false;
            }
        }

    } else {
        if (mHasSilentItem) {
            silentItemChecked = false;
            position--;
        }

        if (mHasDefaultItem) {
            defaultItemChecked = false;
            position--;
        }
    }
    if (id != -1) {
        toneCur.moveToPosition(position);
        Log.e("lys", "onItemClick position == " + position + "id ==" + id);
        long newId = toneCur.getLong(toneCur.getColumnIndex(MediaStore.Audio.Media._ID));
        mSelectedId = newId;
        Log.e("lys", " onItemClick mSelectedId==" + mSelectedId);

        //wuqingliang modify begin20130307
        if (tabName == SYSTEM_TONE) {
            BaseUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
            Log.e("lys", "BaseUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;111111");
        } else {
            BaseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Log.e("lys", "BaseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;1111111");
        }
        //wuqingliang modify end
        mSelectedUri = ContentUris.withAppendedId(BaseUri, newId);
    }
    listView.invalidateViews();
    audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    stopMediaPlayer();
    mMediaPlayer = new MediaPlayer();
    try {
        if (toneType == ALARM_TYPE)
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        else
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
        mMediaPlayer.setDataSource(RingtonePickerActivity.this, mSelectedUri);
        mMediaPlayer.prepare();
        mMediaPlayer.start();

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}