List of usage examples for android.media AudioManager AUDIOFOCUS_GAIN
int AUDIOFOCUS_GAIN
To view the source code for android.media AudioManager AUDIOFOCUS_GAIN.
Click Source Link
From source file:com.thejoshwa.ultrasonic.androidapp.util.Util.java
public static void requestAudioFocus(final Context context) { if (!hasFocus) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); hasFocus = true;// w w w .j ava 2 s .co m audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadServiceImpl downloadService = (DownloadServiceImpl) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) { hasFocus = false; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
From source file:github.daneren2005.dsub.util.Util.java
@TargetApi(8) public static void requestAudioFocus(final Context context) { if (Build.VERSION.SDK_INT >= 8 && !hasFocus) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); hasFocus = true;/*from w w w . j a v a 2 s .co m*/ audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadServiceImpl downloadService = (DownloadServiceImpl) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) { hasFocus = false; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
From source file:br.com.viniciuscr.notification2android.mediaPlayer.MediaPlaybackService.java
/** * Starts playback of a previously opened file. *//* w ww .j a va 2 s. c o m*/ public void play() { mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mAudioManager.registerMediaButtonEventReceiver( new ComponentName(this.getPackageName(), MediaButtonIntentReceiver.class.getName())); if (mPlayer.isInitialized()) { // if we are at the end of the song, go to the next song first long duration = mPlayer.duration(); if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) { gotoNext(true); } mPlayer.start(); // make sure we fade in, in case a previous fadein was stopped because // of another focus loss mMediaplayerHandler.removeMessages(FADEDOWN); mMediaplayerHandler.sendEmptyMessage(FADEUP); updateNotification(); if (!mIsSupposedToBePlaying) { mIsSupposedToBePlaying = true; notifyChange(PLAYSTATE_CHANGED); } } else if (mPlayListLen <= 0) { // This is mostly so that if you press 'play' on a bluetooth headset // without every having played anything before, it will still play // something. setShuffleMode(SHUFFLE_AUTO); } }
From source file:com.rks.musicx.services.MusicXService.java
@Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: Log.d(TAG, "AudioFocus Loss"); if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) { pause();/* w w w . j ava 2s .c om*/ //service.stopSelf(); } break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) { MediaPlayerSingleton.getInstance().getMediaPlayer().setVolume(0.3f, 0.3f); mIsDucked = true; } Log.d(TAG, "AudioFocus Loss Can Duck Transient"); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: Log.d(TAG, "AudioFocus Loss Transient"); if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) { pause(); mLostAudioFocus = true; } break; case AudioManager.AUDIOFOCUS_GAIN: Log.d(TAG, "AudioFocus Gain"); if (mIsDucked) { MediaPlayerSingleton.getInstance().getMediaPlayer().setVolume(1.0f, 1.0f); mIsDucked = false; } else if (mLostAudioFocus) { // If we temporarily lost the audio focus we can resume playback here if (MediaPlayerSingleton.getInstance().getMediaPlayer().isPlaying()) { play(); } mLostAudioFocus = false; } break; default: Log.d(TAG, "Unknown focus"); } }
From source file:github.daneren2005.dsub.util.Util.java
@TargetApi(8) public static void requestAudioFocus(final Context context) { if (Build.VERSION.SDK_INT >= 8 && focusListener == null) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadService downloadService = (DownloadService) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isRemoteEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { Log.i(TAG, "Temporary loss of focus"); SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(true); }/*w ww .ja v a 2 s.c o m*/ } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isRemoteEnabled()) { Log.i(TAG, "Permanently lost focus"); focusListener = null; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
From source file:org.yammp.MusicPlaybackService.java
/** * Starts playback of a previously opened file. *//*from w w w .j a v a2 s . c o m*/ public void play() { CharSequence contentTitle, contentText = null; TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) return; mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mAudioManager.registerMediaButtonEventReceiver( new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName())); telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); if (mPlayer.isInitialized()) { // if we are at the end of the song, go to the next song first long duration = mPlayer.duration(); if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) { next(true); } mPlayer.start(); // make sure we fade in, in case a previous fadein was stopped // because // of another focus loss mMediaplayerHandler.removeMessages(FADEDOWN); mMediaplayerHandler.sendEmptyMessage(FADEUP); contentTitle = getTrackName(); String artist = getArtistName(); boolean isUnknownArtist = artist == null || MediaStore.UNKNOWN_STRING.equals(artist); String album = getAlbumName(); boolean isUnknownAlbum = album == null || MediaStore.UNKNOWN_STRING.equals(album); if (!isUnknownArtist && !isUnknownAlbum) { contentText = getString(R.string.notification_artist_album, artist, album); } else if (isUnknownArtist && !isUnknownAlbum) { contentText = album; } else if (!isUnknownArtist && isUnknownAlbum) { contentText = artist; } PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(INTENT_PLAYBACK_VIEWER), 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setOngoing(true); builder.setSmallIcon(R.drawable.ic_stat_playback); builder.setContentIntent(contentIntent); builder.setContentTitle(contentTitle); builder.setContentText(contentText); mNotificationManager.notify(ID_NOTIFICATION_PLAYBACK, builder.getNotification()); if (!mIsSupposedToBePlaying) { mIsSupposedToBePlaying = true; notifyChange(BROADCAST_PLAYSTATE_CHANGED); } } else if (mPlayListLen <= 0) { // This is mostly so that if you press 'play' on a bluetooth headset // without every having played anything before, it will still play // something. setShuffleMode(SHUFFLE_NORMAL); } }
From source file:com.andrew.apolloMod.service.ApolloService.java
/** * Starts playback of a previously opened file. *///from w w w. j a va 2 s .c om public void play() { mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mAudioManager.registerMediaButtonEventReceiver( new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName())); if (mPlayer.isInitialized()) { // if we are at the end of the song, go to the next song first mPlayer.start(); // make sure we fade in, in case a previous fadein was stopped // because // of another focus loss mMediaplayerHandler.removeMessages(FADEDOWN); mMediaplayerHandler.sendEmptyMessage(FADEUP); updateNotification(); if (!mIsSupposedToBePlaying) { mIsSupposedToBePlaying = true; notifyChange(PLAYSTATE_CHANGED); } } else if (mPlayListLen <= 0) { // This is mostly so that if you press 'play' on a bluetooth headset // without every having played anything before, it will still play // something. setShuffleMode(SHUFFLE_AUTO); } }
From source file:github.madmarty.madsonic.util.Util.java
@TargetApi(8) public static void requestAudioFocus(final Context context) { if (Build.VERSION.SDK_INT >= 8 && !hasFocus) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); hasFocus = true;//from w w w . ja v a2 s.co m audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadServiceImpl downloadService = (DownloadServiceImpl) context; if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isJukeboxEnabled()) { if (downloadService.getPlayerState() == PlayerState.STARTED) { SharedPreferences prefs = getPreferences(context); int lossPref = Integer .parseInt(prefs.getString(Constants.PREFERENCES_KEY_AUDIO_FOCUS, "1")); if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) { lowerFocus = true; downloadService.setVolume(0.1f); } else if (lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; downloadService.pause(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { if (pauseFocus) { pauseFocus = false; downloadService.start(); } else if (lowerFocus) { lowerFocus = false; downloadService.setVolume(1.0f); } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) { hasFocus = false; downloadService.pause(); audioManager.abandonAudioFocus(this); } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
From source file:org.runbuddy.tomahawk.services.PlaybackService.java
/** * Try to get the system audio focus./*from w w w .j a va 2 s . c o m*/ */ private void tryToGetAudioFocus() { Log.d(TAG, "tryToGetAudioFocus"); if (mAudioFocus != AUDIO_FOCUSED) { int result = mAudioManager.requestAudioFocus(mFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mAudioFocus = AUDIO_FOCUSED; } } }
From source file:net.sourceforge.servestream.service.MediaPlaybackService.java
/** * Starts playback of a previously opened file. *///from w ww . j a v a 2 s . c o m public void play() { mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent); if (mPlayer.isInitialized()) { // if we are at the end of the song, go to the next song first /*long duration = mPlayer.duration(); if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) { gotoNext(true); }*/ mPlayer.start(); // make sure we fade in, in case a previous fadein was stopped because // of another focus loss mMediaplayerHandler.removeMessages(FADEDOWN); mMediaplayerHandler.sendEmptyMessage(FADEUP); updateNotification(false); if (!mIsSupposedToBePlaying) { mIsSupposedToBePlaying = true; notifyChange(PLAYSTATE_CHANGED); } } else { openCurrentAndNext(); } }