List of usage examples for android.media AudioManager AUDIOFOCUS_REQUEST_GRANTED
int AUDIOFOCUS_REQUEST_GRANTED
To view the source code for android.media AudioManager AUDIOFOCUS_REQUEST_GRANTED.
Click Source Link
From source file:com.smithdtyler.prettygoodmusicplayer.MusicPlaybackService.java
private synchronized void play() { pauseTime = Long.MAX_VALUE;//from w ww . ja va 2s . c o m if (mp.isPlaying()) { // do nothing } else { // Request audio focus for playback int result = am.requestAudioFocus(MusicPlaybackService.this.audioFocusListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); Log.d(TAG, "requestAudioFocus result = " + result); Log.i(TAG, "About to play " + songFile); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.d(TAG, "We got audio focus!"); mp.start(); updateNotification(); wakeLock.acquire(); } else { Log.e(TAG, "Unable to get audio focus"); } } }
From source file:com.aengbee.android.leanback.ui.PlaybackOverlayCustomFragment.java
private void requestAudioFocus() { if (mHasAudioFocus) { return;/*from ww w.j a v a2 s . c om*/ } int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mHasAudioFocus = true; } else { pause(); } }
From source file:com.example.android.leanback.MediaSessionService.java
private void play() { // Only when player is not null (meaning the player has been created), the player is // prepared (using the mInitialized as the flag to represent it, // this boolean variable will only be assigned to true inside of the onPrepared callback) // and the media item is not currently playing (!isPlaying()), then the player can be // started.//w w w . ja v a2 s . co m // If the player has not been prepared, but this function is fired, it is an error state // from the app side if (!mInitialized) { PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(PlaybackStateCompat.STATE_ERROR); builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, PLAYER_NOT_INITIALIZED); mMediaSession.setPlaybackState(builder.build()); // If the player has is playing, and this function is fired again, it is an error state // from the app side } else { // Request audio focus only when needed if (mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { return; } if (mPlayer.getPlaybackParams().getSpeed() != NORMAL_SPEED) { // Reset to normal speed and play resetSpeedAndPlay(); } else { // Continue play. mPlayer.start(); mMediaSession .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).build()); } } }
From source file:com.mine.psf.PsfPlaybackService.java
private void requestAudioFocus() { // Request audio focus for playback int result = PsfAudioManager.requestAudioFocus(AudioFocusChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC,//from w w w . jav a 2 s. c o m // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.d(LOGTAG, "Audio focus granted"); } else { Log.w(LOGTAG, "Audio focus not granted: " + result); } }
From source file:com.gelakinetic.mtgfam.fragments.LifeCounterFragment.java
/** * Build a LinkedList of all the things to say, which can include TTS calls and MediaPlayer calls. Then call * onUtteranceCompleted to start running through the LinkedList, even though no utterance was spoken. *//*from w ww. j a v a2 s . c om*/ private void announceLifeTotals() { if (mTtsInit) { mVocalizations.clear(); for (LcPlayer p : mPlayers) { switch (mStatDisplaying) { case STAT_LIFE: if (p.mLife > 9000) { /* If the life is over 9000, split the string on an IMPROBABLE_NUMBER, and insert a call to the m9000Player */ String tmp = getResources().getQuantityString(R.plurals.life_counter_spoken_life, IMPROBABLE_NUMBER, p.mName, IMPROBABLE_NUMBER); String parts[] = tmp.split(Integer.toString(IMPROBABLE_NUMBER)); mVocalizations.add(parts[0]); mVocalizations.add(OVER_9000_KEY); mVocalizations.add(parts[1]); } else { mVocalizations.add(getResources().getQuantityString(R.plurals.life_counter_spoken_life, p.mLife, p.mName, p.mLife)); } break; case STAT_POISON: mVocalizations.add(getResources().getQuantityString(R.plurals.life_counter_spoken_poison, p.mPoison, p.mName, p.mPoison)); break; } } if (mVocalizations.size() > 0) { /* Get the audio focus, and tell everyone else to be quiet for a moment */ int res = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { onUtteranceCompleted(LIFE_ANNOUNCE); } } } }
From source file:com.android.tv.MainActivity.java
@Override protected void onResume() { if (DEBUG)//from www .j a v a 2s.c om Log.d(TAG, "onResume()"); super.onResume(); if (!PermissionUtils.hasAccessAllEpg(this) && checkSelfPermission(PERMISSION_READ_TV_LISTINGS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[] { PERMISSION_READ_TV_LISTINGS }, PERMISSIONS_REQUEST_READ_TV_LISTINGS); } mTracker.sendScreenView(SCREEN_NAME); SystemProperties.updateSystemProperties(); mNeedShowBackKeyGuide = true; mActivityResumed = true; mShowNewSourcesFragment = true; mOtherActivityLaunched = false; int result = mAudioManager.requestAudioFocus(MainActivity.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mAudioFocusStatus = (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) ? AudioManager.AUDIOFOCUS_GAIN : AudioManager.AUDIOFOCUS_LOSS; setVolumeByAudioFocusStatus(); if (mTvView.isPlaying()) { // Every time onResume() is called the activity will be assumed to not have requested // visible behind. requestVisibleBehind(true); } if (mChannelTuner.areAllChannelsLoaded()) { SetupUtils.getInstance(this).markNewChannelsBrowsable(); resumeTvIfNeeded(); resumePipIfNeeded(); } mOverlayManager.showMenuWithTimeShiftPauseIfNeeded(); // Note: The following codes are related to pop up an overlay UI after resume. // When the following code is changed, please check the variable // willShowOverlayUiAfterResume in updateChannelBannerAndShowIfNeeded. if (mInputToSetUp != null) { startSetupActivity(mInputToSetUp, false); mInputToSetUp = null; } else if (mShowProgramGuide) { mShowProgramGuide = false; mHandler.post(new Runnable() { // This will delay the start of the animation until after the Live Channel app is // shown. Without this the animation is completed before it is actually visible on // the screen. @Override public void run() { mOverlayManager.showProgramGuide(); } }); } else if (mShowSelectInputView) { mShowSelectInputView = false; mHandler.post(new Runnable() { // mShowSelectInputView is true when the activity is started/resumed because the // TV_INPUT button was pressed in a different app. // This will delay the start of the animation until after the Live Channel app is // shown. Without this the animation is completed before it is actually visible on // the screen. @Override public void run() { mOverlayManager.showSelectInputView(); } }); } }
From source file:androidx.media.widget.VideoView2.java
@SuppressWarnings("deprecation") private void requestAudioFocus(int focusType) { int result;/*www.j a va 2 s . c om*/ if (android.os.Build.VERSION.SDK_INT >= 26) { AudioFocusRequest focusRequest; focusRequest = new AudioFocusRequest.Builder(focusType).setAudioAttributes(mAudioAttributes) .setOnAudioFocusChangeListener(mAudioFocusListener).build(); result = mAudioManager.requestAudioFocus(focusRequest); } else { result = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, focusType); } if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) { mAudioFocused = false; } else if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mAudioFocused = true; } else if (result == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) { mAudioFocused = false; } }
From source file:org.runbuddy.tomahawk.services.PlaybackService.java
/** * Try to get the system audio focus.//from www. j a v a 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:org.runbuddy.tomahawk.services.PlaybackService.java
/** * Give up the audio focus./*from w w w . j ava2 s.c o m*/ */ private void giveUpAudioFocus() { Log.d(TAG, "giveUpAudioFocus"); if (mAudioFocus == AUDIO_FOCUSED) { if (mAudioManager.abandonAudioFocus(mFocusChangeListener) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK; } } }
From source file:com.example.chu.myvideodemo.MusicService.java
public void play(boolean createNewNextTrack) { int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (D)/* w w w .j av a 2s . c om*/ Log.d(TAG, "Starting playback: audio focus request status = " + status); if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { return; } final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId()); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(intent); mAudioManager.registerMediaButtonEventReceiver( new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName())); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) mSession.setActive(true); if (createNewNextTrack) { setNextTrack(); } else { setNextTrack(mNextPlayPos); } if (mPlayer.isInitialized()) { final long duration = mPlayer.duration(); if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) { gotoNext(true); } mPlayer.start(); mPlayerHandler.removeMessages(FADEDOWN); mPlayerHandler.sendEmptyMessage(FADEUP); setIsSupposedToBePlaying(true, true); cancelShutdown(); //updateNotification(); notifyChange(META_CHANGED); } else if (mPlaylist.size() <= 0) { setShuffleMode(SHUFFLE_AUTO); } }