List of usage examples for android.media AudioManager requestAudioFocus
public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint)
From source file:com.ecoplayer.beta.MusicService.java
private boolean initMediaPlayer() { startForeground(NOTIFICATION_ID, notiBuilder.getNotification()); AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); int result = audioManager.requestAudioFocus(audioFocusChangeList, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mMediaPlayer = new MediaPlayer(); // initialize it here mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnCompletionListener(onCompletion); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // prevent CPU from going to sleep mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); return true; } else {/*from w w w. ja v a 2 s . c o m*/ Log.w(LOG_TAG, "The service hasn't got the audio focus"); } return false; }
From source file:com.customprogrammingsolutions.MediaStreamer.MediaStreamerService.java
private boolean requestAudioFocus() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) return false; else//from w w w. j a v a 2 s . co m return true; }
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 . j a va2 s . com*/ } } 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: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;/*from www.j av a2s . c om*/ 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:com.evandroid.musica.MainLyricActivity.java
@SuppressLint("InlinedApi") public void resync(MenuItem item) { AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); am.requestAudioFocus(null, // Use the music stream. AudioManager.STREAM_SYSTEM,//from ww w . j a v a 2s .c om // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); am.abandonAudioFocus(null); }
From source file:com.mylovemhz.simplay.MusicService.java
private void cueTrack() throws IOException, IndexOutOfBoundsException { Log.d(TAG_MUSIC_SERVICE, "Cue Track..."); AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result != AudioManager.AUDIOFOCUS_GAIN) { return; //Failed to gain audio focus }/*from w w w . ja v a2s .c om*/ if (getCurrentState() == State.STOPPED) { mediaPlayer.reset(); currentState = State.IDLE; } if (getCurrentState() == State.IDLE) { if (trackQueue.size() == 0) { return; //nothing to play } if (hasPermission(Manifest.permission.WAKE_LOCK)) { if (!wifiLock.isHeld()) { wifiLock.acquire(); } Track track = trackQueue.get(currentTrackIndex); mediaPlayer.setDataSource(track.getUrl()); currentState = State.INITIALIZED; mediaPlayer.prepareAsync(); currentState = State.PREPARING; if (callbacks != null) callbacks.onLoading(); } else { Log.e(TAG_MUSIC_SERVICE, "need permission " + Manifest.permission.WAKE_LOCK); if (callbacks != null) { callbacks.onPermissionRequired(REQUEST_PERMISSION_WAKE_LOCK, Manifest.permission.WAKE_LOCK, RATIONALE_WAKE_LOCK); } } } }
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;//w w w . ja va 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_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: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:org.videolan.vlc.AudioService.java
@TargetApi(8) private void changeAudioFocus(boolean gain) { if (!Util.isFroyoOrLater()) // NOP if not supported return;/* ww w.java2 s . c o m*/ audioFocusListener = new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) { /* * Lower the volume to 36% to "duck" when an alert or something * needs to be played. */ LibVLC.getExistingInstance().setVolume(36); } else { LibVLC.getExistingInstance().setVolume(100); } } }; 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.amahi.anywhere.service.AudioService.java
private void setUpAudioPlayerRemote() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName()); Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); audioIntent.setComponent(audioReceiver); PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0); audioPlayerRemote = new RemoteControlClient(audioPendingIntent); audioPlayerRemote.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS); audioPlayerRemote.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); audioManager.registerMediaButtonEventReceiver(audioReceiver); audioManager.registerRemoteControlClient(audioPlayerRemote); }