Java Code Examples for android.media.AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK

The following examples show how to use android.media.AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VoiceMediator.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    Log.i("LingJu", "焦点状态:" + focusChange);
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            isFocus = false;
            mAudioManager.abandonAudioFocus(mFocusChangeListener);
            break;
        case AudioManager.AUDIOFOCUS_GAIN:
            Log.i("LingJu", "MainActivity onAudioFocusChange()");
            isFocus = true;
            break;
    }
}
 
Example 2
Source File: ApplozicAudioManager.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onAudioFocusChange(int i) {
    switch (i) {
        case AudioManager.AUDIOFOCUS_GAIN:
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            pauseIfPlaying();
            if (currentView != null) {
                currentView.setAudioIcons();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            pauseIfPlaying();
            if (currentView != null) {
                currentView.setAudioIcons();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            pauseIfPlaying();
            if (currentView != null) {
                currentView.setAudioIcons();
            }
            break;
    }
}
 
Example 3
Source File: RadioPlayerService.java    From monkeyboard-radio-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focus){
    Log.v(TAG, "Audio focus changed");
    switch(focus) {
        case AudioManager.AUDIOFOCUS_LOSS:
            // Another app has gained focus;
            handleFocusLost();
            break;
        case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
            handlePauseRequest();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            if (radio.isAttached()) {
                handleFocusDuck();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            if (radio.isAttached()) {
                handleMuteRequest();
            }
            break;
        case AudioManager.AUDIOFOCUS_GAIN:
            handleFocusGain();
            break;
    }
}
 
Example 4
Source File: AudioTrackManagerDualStreamType.java    From apollo-DuerOS with Apache License 2.0 6 votes vote down vote up
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_LOSS:
            LogUtil.d(TAG, "tts AUDIOFOCUS_LOSS");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            setTTSAudioFocusStatus(false);
            mAM.abandonAudioFocus(mTTSAudioFocusListener);
            LogUtil.d(TAG, "tts AUDIOFOCUS_LOSS_TRANSIENT");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            setTTSAudioFocusStatus(false);

            LogUtil.d(TAG, "tts AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
            break;
        case AudioManager.AUDIOFOCUS_GAIN:
            setTTSAudioFocusStatus(true);
            LogUtil.d(TAG, "tts AUDIOFOCUS_GAIN");
            break;

        default:
            break;
    }
}
 
Example 5
Source File: MusicControlAudioFocusListener.java    From react-native-music-control with MIT License 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
        abandonAudioFocus();
        mPlayOnAudioFocus = false;
        emitter.onStop();
    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
        if (MusicControlModule.INSTANCE.isPlaying()) {
            mPlayOnAudioFocus = true;
            emitter.onPause();
        }
    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        volume.setCurrentVolume(40);
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        if (volume.getCurrentVolume() != 100) {
            volume.setCurrentVolume(100);
        }
        if (mPlayOnAudioFocus) {
            emitter.onPlay();
        }
        mPlayOnAudioFocus = false;
    }
}
 
Example 6
Source File: JZVideoPlayer.java    From JZVideoDemo with MIT License 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            releaseAllVideos();
            Log.d(TAG, "AUDIOFOCUS_LOSS [" + this.hashCode() + "]");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            try {
                JZVideoPlayer player = JZVideoPlayerManager.getCurrentJzvd();
                if (player != null && player.currentState == JZVideoPlayer.CURRENT_STATE_PLAYING) {
                    player.startButton.performClick();
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT [" + this.hashCode() + "]");
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            break;
    }
}
 
Example 7
Source File: MediaSnippetsActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
public void onAudioFocusChange(int focusChange) {
  AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  switch (focusChange) {
    case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
      // Lower the volume while ducking.
      mediaPlayer.setVolume(0.2f, 0.2f);
      break;
    case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT):
      mediaPlayer.pause();
      break;
    case (AudioManager.AUDIOFOCUS_LOSS):
      mediaPlayer.stop();
      am.abandonAudioFocus(this);
      break;
    case (AudioManager.AUDIOFOCUS_GAIN):
      // Return the volume to normal and resume if paused.
      mediaPlayer.setVolume(1f, 1f);
      mediaPlayer.start();
      break;
    default:
      break;
  }
}
 
Example 8
Source File: IflySynthesizer.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    Log.e(TAG, "audioFocusChangeListener.onAudioFocusChange>>>>>>>>>>>>>>>>>>" + focusChange);
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            // pauseSpeaking();
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            /*if (isSpeaking()) {
                synthesizer.resumeSpeaking();
            }*/
            break;
        default:
            break;
    }
}
 
Example 9
Source File: GSYVideoView.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            onGankAudio();
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            onLossAudio();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            onLossTransientAudio();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            onLossTransientCanDuck();
            break;
    }
}
 
Example 10
Source File: FocusRequester.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static String focusChangeToString(int focus) {
    switch(focus) {
        case AudioManager.AUDIOFOCUS_NONE:
            return "none";
        case AudioManager.AUDIOFOCUS_GAIN:
            return "GAIN";
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            return "GAIN_TRANSIENT";
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
            return "GAIN_TRANSIENT_MAY_DUCK";
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
            return "GAIN_TRANSIENT_EXCLUSIVE";
        case AudioManager.AUDIOFOCUS_LOSS:
            return "LOSS";
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            return "LOSS_TRANSIENT";
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            return "LOSS_TRANSIENT_CAN_DUCK";
        default:
            return "[invalid focus change" + focus + "]";
    }
}
 
Example 11
Source File: MediaPlayerService.java    From AudioAnchor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onAudioFocusChange(int focusState) {
    Log.e("MediaPlayerService", "calling onAudioFocusChange()");
    // Invoked when the audio focus of the system is updated.
    switch (focusState) {
        case AudioManager.AUDIOFOCUS_GAIN:
            // Resume playback
            Log.e("MediaPlayerService", "Audiofocus Gain");
            if (mMediaPlayer == null) {
                initMediaPlayer(mActiveAudio.getPath(), mActiveAudio.getCompletedTime());
            }
            setVolume(1.0f);
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            // Lost focus for an unbounded amount of time: stop playback and release media player
            Log.e("MediaPlayerService", "Audiofocus Loss");
            pause();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            // Lost focus for a short time, but we have to stop
            // playback. We don't release the media player because playback
            // is likely to resume
            Log.e("MediaPlayerService", "Audiofocus loss transient");

            if (mMediaPlayer.isPlaying()) mMediaPlayer.pause();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            // Lost focus for a short time, but it's ok to keep playing
            // at an attenuated level
            Log.e("MediaPlayerService", "Audiofocus loss can duck");
            if (mMediaPlayer.isPlaying()) setVolume(0.1f);
            break;
    }
}
 
Example 12
Source File: ExoVideoView.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    if (!handleAudioFocus || currentFocus == focusChange) {
        return;
    }

    currentFocus = focusChange;
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            if (startRequested || pausedForLoss) {
                start();
                startRequested = false;
                pausedForLoss = false;
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            if (isPlaying()) {
                pausedForLoss = true;
                pause();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            if (isPlaying()) {
                pausedForLoss = true;
                pause(true);
            }
            break;
    }
}
 
Example 13
Source File: PlayerAdapter.java    From android-MediaBrowserService with Apache License 2.0 5 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            if (mPlayOnAudioFocus && !isPlaying()) {
                play();
            } else if (isPlaying()) {
                setVolume(MEDIA_VOLUME_DEFAULT);
            }
            mPlayOnAudioFocus = false;
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            setVolume(MEDIA_VOLUME_DUCK);
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            if (isPlaying()) {
                mPlayOnAudioFocus = true;
                pause();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            mAudioManager.abandonAudioFocus(this);
            mPlayOnAudioFocus = false;
            stop();
            break;
    }
}
 
Example 14
Source File: RNJWPlayerView.java    From react-native-jw-media-player with MIT License 5 votes vote down vote up
public void onAudioFocusChange(int focusChange) {
    if (mPlayer != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    if (playbackDelayed || !userPaused) {
                        synchronized(focusLock) {
                            playbackDelayed = false;
                        }
                        boolean autostart = mPlayer.getConfig().getAutostart();
                        if (autostart) {
                            mPlayer.play();
                        }
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    synchronized(focusLock) {
                        wasInterrupted = true;
                        playbackDelayed = false;
                    }
                    mPlayer.pause();
                    hasAudioFocus = false;
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    synchronized(focusLock) {
                        wasInterrupted = true;
                        playbackDelayed = false;
                    }
                    mPlayer.pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    // ... pausing or ducking depends on your app
                    break;
            }
        } else {
            lowerApiOnAudioFocus(focusChange);
        }
    }
}
 
Example 15
Source File: AudioFocusManager.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        // 重新获得焦点
        case AudioManager.AUDIOFOCUS_GAIN:
            if (isPausedByFocusLossTransient) {
                // 通话结束,恢复播放
                onFocusGainFromFocusLossTransient();
            }else {
                // 恢复音量
                onFocusGain();
            }
            isPausedByFocusLossTransient = false;
            Logger.d(TAG, "重新获得焦点");
            break;
        // 永久丢失焦点,如被其他播放器抢占
        case AudioManager.AUDIOFOCUS_LOSS:
            onFocusLoss();
            abandonAudioFocus();
            Logger.d(TAG, "永久丢失焦点,如被其他播放器抢占");
            break;
        // 短暂丢失焦点,如来电
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            onFocusLossTransient();
            isPausedByFocusLossTransient = true;
            Logger.d(TAG, "短暂丢失焦点,如来电");
            break;
        // 瞬间丢失焦点,如通知
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            // 音量减小为一半
            onFocusLossTransientCanDuck();
            Logger.d(TAG, "瞬间丢失焦点,如通知");
            break;
        default:
            break;
    }
}
 
Example 16
Source File: MusicPlaybackService.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
    switch (msg.what) {
        case FOCUS_CHANGE:
            switch (msg.arg1)
            {
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener) !=
                            AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                        Log.w(TAG, "abandonAudioFocus after AudioFocus_LOSS failed!");
                    }
                    pause();
                    Log.d(TAG, "AudioFocus: Received AUDIOFOCUS_LOSS.");
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    Log.d(TAG, "AudioFocus: Received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK.");
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    Log.d(TAG, "AudioFocus: Received AUDIOFOCUS_LOSS_TRANSIENT.");
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                    Log.d(TAG, "AudioFocus: Received AUDIOFOCUS_GAIN.");
                    break;
            }
            break;
    }
}
 
Example 17
Source File: IntentPlayer.java    From intent_radio with MIT License 5 votes vote down vote up
@Override
public void onAudioFocusChange(int change)
{
   log("onAudioFocusChange: ", ""+change);

   if ( player != null )
      switch (change)
      {
         case AudioManager.AUDIOFOCUS_GAIN:
            log("Audiofocus_gain");
            restart();
            break;

         case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            log("Transient");
            // pause();
            // break;
            // Drop through.

         case AudioManager.AUDIOFOCUS_LOSS:
            log("Audiofocus_loss");
            pause();
            break;

         case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            log("Audiofocus_loss_transient_can_duck");
            duck("Audio focus lost, ducking...");
            break;
      }
}
 
Example 18
Source File: BaseIjkVideoView.java    From youqu_master with Apache License 2.0 5 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    if (currentFocus == focusChange) {
        return;
    }

    currentFocus = focusChange;
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN://获得焦点
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT://暂时获得焦点
            if (startRequested || pausedForLoss) {
                start();
                startRequested = false;
                pausedForLoss = false;
            }
            if (mMediaPlayer != null && !isMute)//恢复音量
                mMediaPlayer.setVolume(1.0f, 1.0f);
            break;
        case AudioManager.AUDIOFOCUS_LOSS://焦点丢失
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT://焦点暂时丢失
            if (isPlaying()) {
                pausedForLoss = true;
                pause();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK://此时需降低音量
            if (mMediaPlayer != null && isPlaying() && !isMute) {
                mMediaPlayer.setVolume(0.1f, 0.1f);
            }
            break;
    }
}
 
Example 19
Source File: RadioPlaybackService.java    From Sky31Radio with Apache License 2.0 5 votes vote down vote up
@Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
                // resume playback
                Timber.i("AudioManager.AUDIOFOCUS_GAIN");
//                player.play();
                break;

            case AudioManager.AUDIOFOCUS_LOSS:
                // Lost focus for an unbounded amount of time: stop playback and release media player
                Timber.i("AudioManager.AUDIOFOCUS_LOSS");
                if(player!=null){
                    player.stop();
                }
                break;

            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                // Lost focus for a short time, but we have to stop
                // playback. We don't release the media player because playback
                // is likely to resume
                Timber.i("AudioManager.AUDIOFOCUS_LOSS_TRANSIENT");
                if(player!=null){
                    player.pause();
                }
                break;

            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                // Lost focus for a short time, but it's ok to keep playing
                // at an attenuated level
                Timber.i("AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                break;
        }
    }
 
Example 20
Source File: LocalPlayback.java    From klingar with Apache License 2.0 5 votes vote down vote up
@Override public void onAudioFocusChange(int focusChange) {
  Timber.d("onAudioFocusChange focusChange %s", focusChange);

  switch (focusChange) {
    case AudioManager.AUDIOFOCUS_GAIN:
      audioFocus = AUDIO_FOCUSED;
      break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
      // Audio focus was lost, but it's possible to duck (i.e.: play quietly)
      audioFocus = AUDIO_NO_FOCUS_CAN_DUCK;
      break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
      // Lost audio focus, but will gain it back (shortly), so note whether
      // playback should resume
      audioFocus = AUDIO_NO_FOCUS_NO_DUCK;
      playOnFocusGain = exoPlayer != null && exoPlayer.getPlayWhenReady();
      break;
    case AudioManager.AUDIOFOCUS_LOSS:
      // Lost audio focus, probably "permanently"
      audioFocus = AUDIO_NO_FOCUS_NO_DUCK;
      break;
    default:
      break;
  }

  if (exoPlayer != null) {
    // Update the player state based on the change
    configurePlayerState();
  }
}