Example usage for android.media AudioManager AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK

List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK

Introduction

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

Prototype

int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK

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

Click Source Link

Document

Used to indicate a temporary request of audio focus, anticipated to last a short amount of time, and where it is acceptable for other audio applications to keep playing after having lowered their output level (also referred to as "ducking").

Usage

From source file:org.chromium.tools.audio_focus_grabber.AudioFocusGrabberListenerService.java

void processIntent(Intent intent) {
    if (mMediaPlayer != null) {
        Log.i(TAG,/*from   w ww. j av  a  2  s. c o  m*/
                "There's already a MediaPlayer playing," + " stopping the existing player and abandon focus");
        releaseAndAbandonAudioFocus();
    }
    String action = intent.getAction();
    if (ACTION_SHOW_NOTIFICATION.equals(action)) {
        showNotification();
    } else if (ACTION_HIDE_NOTIFICATION.equals(action)) {
        hideNotification();
    } else if (ACTION_GAIN.equals(action)) {
        gainFocusAndPlay(AudioManager.AUDIOFOCUS_GAIN);
    } else if (ACTION_TRANSIENT_PAUSE.equals(action)) {
        gainFocusAndPlay(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    } else if (ACTION_TRANSIENT_DUCK.equals(action)) {
        gainFocusAndPlay(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
    } else {
        assert false;
    }
}

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

/**
 * Updates the JavaScript side with new audio focus event
 *
 * @param focusEvent the received event/*from   w w w .  j  ava  2 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:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java

private void nativeRingtone(Context context, boolean hasSound) {
    if (hasSound) {
        final AudioManager.OnAudioFocusChangeListener listener = this;
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        int result = audioManager.requestAudioFocus(listener, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {

            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mRingtone = RingtoneManager.getRingtone(context, notification);
            mRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
            mRingtone.play();/*from w  w w  .  ja  v a 2 s. c  o  m*/

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    audioManager.abandonAudioFocus(listener);
                }
            }, AUDIO_FOCUS_DURATION);
        }
    }
}

From source file:org.fdroid.enigtext.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {// w  w w.  j  av a 2s  .  c  om
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        if (!sp.getBoolean(ApplicationPreferencesActivity.IN_THREAD_NOTIFICATION_PREF, true)) {
            return;
        }

        String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:com.securecomcode.text.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {/* w  ww  . j a va 2s .  c o  m*/
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        String ringtone = TextSecurePreferences.getNotificationRingtone(context);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    Log.i(TAG, "onCreateView");
    init();//  w  w w.  j av  a 2s  .c  om
    View view = inflater.inflate(R.layout.audio, container, false);
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioFocusHandler = new FocusHandler((RadioGroup) view.findViewById(R.id.button_focus_request_selection),
            (Button) view.findViewById(R.id.button_audio_focus_request),
            (TextView) view.findViewById(R.id.text_audio_focus_state));
    mMediaPlay = (Button) view.findViewById(R.id.button_media_play_start);
    mMediaPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean requestFocus = true;
            boolean repeat = true;
            mMusicPlayer.start(requestFocus, repeat, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mMediaPlayOnce = (Button) view.findViewById(R.id.button_media_play_once);
    mMediaPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayerShort.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            // play only for 1 sec and stop
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mMusicPlayerShort.stop();
                }
            }, 1000);
        }
    });
    mMediaStop = (Button) view.findViewById(R.id.button_media_play_stop);
    mMediaStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayer.stop();
        }
    });
    mWavPlay = (Button) view.findViewById(R.id.button_wav_play_start);
    mWavPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.start(true, true, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mWavStop = (Button) view.findViewById(R.id.button_wav_play_stop);
    mWavStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.stop();
        }
    });
    mNavPlayOnce = (Button) view.findViewById(R.id.button_nav_play_once);
    mNavPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "Nav start");
            }
            if (!mNavGuidancePlayer.isPlaying()) {
                try {
                    mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION,
                            mOwnershipCallbacks);
                } catch (CarNotConnectedException e) {
                    Log.e(TAG, "Failed to set active focus", e);
                }
                mNavGuidancePlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                        new PlayStateListener() {
                            @Override
                            public void onCompletion() {
                                mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                        CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
                            }
                        });
            }
        }
    });
    mVrPlayOnce = (Button) view.findViewById(R.id.button_vr_play_once);
    mVrPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "VR start");
            }
            try {
                mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND,
                        mOwnershipCallbacks);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Failed to set active focus", e);
            }
            if (!mVrPlayer.isPlaying()) {
                mVrPlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, new PlayStateListener() {
                    @Override
                    public void onCompletion() {
                        mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
                    }
                });
            }
        }
    });
    mSystemPlayOnce = (Button) view.findViewById(R.id.button_system_play_once);
    mSystemPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "System start");
            }
            if (!mSystemPlayer.isPlaying()) {
                // system sound played without focus
                mSystemPlayer.start(false, false, 0);
            }
        }
    });
    mNavStart = (Button) view.findViewById(R.id.button_nav_start);
    mNavStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavStart();
        }
    });
    mNavEnd = (Button) view.findViewById(R.id.button_nav_end);
    mNavEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavEnd();
        }
    });
    mVrStart = (Button) view.findViewById(R.id.button_vr_start);
    mVrStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrStart();
        }
    });
    mVrEnd = (Button) view.findViewById(R.id.button_vr_end);
    mVrEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrEnd();
        }
    });
    mRadioStart = (Button) view.findViewById(R.id.button_radio_start);
    mRadioStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioStart();
        }
    });
    mRadioEnd = (Button) view.findViewById(R.id.button_radio_end);
    mRadioEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioEnd();
        }
    });
    mSpeakerPhoneOn = (Button) view.findViewById(R.id.button_speaker_phone_on);
    mSpeakerPhoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(true);
        }
    });
    mSpeakerPhoneOff = (Button) view.findViewById(R.id.button_speaker_phone_off);
    mSpeakerPhoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(false);
        }
    });
    mMicrophoneOn = (Button) view.findViewById(R.id.button_microphone_on);
    mMicrophoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(false); // Turn the microphone on.
        }
    });
    mMicrophoneOff = (Button) view.findViewById(R.id.button_microphone_off);
    mMicrophoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(true); // Mute the microphone.
        }
    });

    mRejectFocus = (ToggleButton) view.findViewById(R.id.button_reject_audio_focus);
    mRejectFocus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                return;
            }
            if (!mEnableMocking.isChecked()) {
                return;
            }
            if (isChecked) {
                mCarEmulator.setAudioFocusControl(true);
            } else {
                mCarEmulator.setAudioFocusControl(false);
            }
        }
    });
    mRejectFocus.setActivated(false);
    mEnableMocking = (ToggleButton) view.findViewById(R.id.button_mock_audio);
    mEnableMocking.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                //TODO(pavelm): need to do a full switch between emulated and normal mode
                // all Car*Manager references should be invalidated.
                Toast.makeText(AudioTestFragment.this.getContext(), "Not supported yet :(", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            if (isChecked) {
                mRejectFocus.setActivated(true);
                mCarEmulator.start();
            } else {
                mRejectFocus.setActivated(false);
                mCarEmulator.stop();
                mCarEmulator = null;
            }
        }
    });
    mMuteMedia = (ToggleButton) view.findViewById(R.id.button_mute_media);
    mMuteMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            try {
                if (isChecked) {
                    mCarAudioManager.setMediaMute(true);
                } else {
                    mCarAudioManager.setMediaMute(false);
                }
            } catch (CarNotConnectedException e) {
                //ignore
            }
        }
    });
    return view;
}

From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context, Recipients recipients) {
    try {//from   w ww.  j av  a  2  s.  c o  m
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        Uri uri = recipients.getRingtone();

        if (uri == null) {
            String ringtone = TextSecurePreferences.getNotificationRingtone(context);

            if (ringtone == null) {
                Log.w(TAG, "ringtone preference was null.");
                return;
            } else {
                uri = Uri.parse(ringtone);
            }
        }

        if (uri == null) {
            Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
            return;
        }

        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:org.videolan.vlc.audio.AudioService.java

@TargetApi(Build.VERSION_CODES.FROYO)
private void changeAudioFocus(boolean gain) {
    if (!LibVlcUtil.isFroyoOrLater()) // NOP if not supported
        return;//from   ww  w .j av a 2s.c o m

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                LibVLC libVLC = LibVLC.getExistingInstance();
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (libVLC.isPlaying())
                        libVLC.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.
                     */
                    libVLC.setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    libVLC.setVolume(100);
                    break;
                }
            }
        };
    }

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

}

From source file:com.dzt.musicplay.player.AudioService.java

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

    if (audioFocusListener == null) {
        audioFocusListener = new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                LibVLC libVLC = LibVLC.getExistingInstance();
                switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (libVLC.isPlaying())
                        libVLC.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.
                     */
                    libVLC.setVolume(36);
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    libVLC.setVolume(100);
                    break;
                }
            }
        };
    }

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

}

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;//from  w  w w  . j a  v  a2  s.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);

}