Java Code Examples for android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT

The following examples show how to use android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT . 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: 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 2
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 3
Source File: FocusRequester.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * For a given audio focus gain request, return the audio focus loss type that will result
 * from it, taking into account any previous focus loss.
 * @param gainRequest
 * @return the audio focus loss type that matches the gain request
 */
private int focusLossForGainRequest(int gainRequest) {
    switch(gainRequest) {
        case AudioManager.AUDIOFOCUS_GAIN:
            switch(mFocusLossReceived) {
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_LOSS:
                case AudioManager.AUDIOFOCUS_NONE:
                    return AudioManager.AUDIOFOCUS_LOSS;
            }
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            switch(mFocusLossReceived) {
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                case AudioManager.AUDIOFOCUS_NONE:
                    return AudioManager.AUDIOFOCUS_LOSS_TRANSIENT;
                case AudioManager.AUDIOFOCUS_LOSS:
                    return AudioManager.AUDIOFOCUS_LOSS;
            }
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
            switch(mFocusLossReceived) {
                case AudioManager.AUDIOFOCUS_NONE:
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    return AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    return AudioManager.AUDIOFOCUS_LOSS_TRANSIENT;
                case AudioManager.AUDIOFOCUS_LOSS:
                    return AudioManager.AUDIOFOCUS_LOSS;
            }
        default:
            Log.e(TAG, "focusLossForGainRequest() for invalid focus request "+ gainRequest);
                    return AudioManager.AUDIOFOCUS_NONE;
    }
}
 
Example 4
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 5
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 6
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 7
Source File: FlutterIncallManagerPlugin.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
@Override
public void onAudioFocusChange(final int focusChange) {
    String focusChangeStr;
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            focusChangeStr = "AUDIOFOCUS_GAIN";
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            focusChangeStr = "AUDIOFOCUS_GAIN_TRANSIENT";
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
            focusChangeStr = "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE";
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
            focusChangeStr = "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK";
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            focusChangeStr = "AUDIOFOCUS_LOSS";
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            focusChangeStr = "AUDIOFOCUS_LOSS_TRANSIENT";
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            focusChangeStr = "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK";
            break;
        default:
            focusChangeStr = "AUDIOFOCUS_UNKNOW";
            break;
    }

    Log.d(TAG, "onAudioFocusChange: " + focusChange + " - " + focusChangeStr);
    if (eventSink != null) {
        ConstraintsMap params = new ConstraintsMap();
        params.putString("event", "onAudioFocusChange");
        params.putString("eventText", focusChangeStr);
        params.putInt("eventCode", focusChange);
        eventSink.success(params.toMap());
    }
}
 
Example 8
Source File: XVideoView.java    From XPlayer2 with Apache License 2.0 5 votes vote down vote up
@Override
    public void onAudioFocusChange(int focusChange) {
//        MLog.i("focusChange = " + focusChange);
        if (currentFocus == focusChange) {
            return;
        }

        currentFocus = focusChange;
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
            case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                // 获得了Audio Focus
                if (!isPlaying()) {
                    play();
                }
                break;
            case AudioManager.AUDIOFOCUS_LOSS:
                // 失去了Audio Focus,并将会持续很长的时间。
                // 这里因为可能会停掉很长时间,所以不仅仅要停止Audio的播放,最好直接释放掉Media资源。
                if (isPlaying()) {
                    stop();
                }
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                // 暂时失去Audio Focus,并会很快再次获得。
                // 必须停止Audio的播放,但是因为可能会很快再次获得AudioFocus,这里可以不释放Media资源;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                // 暂时失去AudioFocus,但是可以继续播放,不过要在降低音量。
                if (isPlaying()) {
                    stop();
                }
                break;
        }
    }
 
Example 9
Source File: AudioFocusHelper.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
private void handleAudioFocusChange(int focusChange) {
    final VideoView videoView = mWeakVideoView.get();
    if (videoView == null) {
        return;
    }
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN://获得焦点
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT://暂时获得焦点
            if (mStartRequested || mPausedForLoss) {
                videoView.start();
                mStartRequested = false;
                mPausedForLoss = false;
            }
            if (!videoView.isMute())//恢复音量
                videoView.setVolume(1.0f, 1.0f);
            break;
        case AudioManager.AUDIOFOCUS_LOSS://焦点丢失
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT://焦点暂时丢失
            if (videoView.isPlaying()) {
                mPausedForLoss = true;
                videoView.pause();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK://此时需降低音量
            if (videoView.isPlaying() && !videoView.isMute()) {
                videoView.setVolume(0.1f, 0.1f);
            }
            break;
    }
}
 
Example 10
Source File: InCallManagerModule.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
@Override
public void onAudioFocusChange(final int focusChange) {
    String focusChangeStr;
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            focusChangeStr = "AUDIOFOCUS_GAIN";
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
            focusChangeStr = "AUDIOFOCUS_GAIN_TRANSIENT";
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
            focusChangeStr = "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE";
            break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
            focusChangeStr = "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK";
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            focusChangeStr = "AUDIOFOCUS_LOSS";
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            focusChangeStr = "AUDIOFOCUS_LOSS_TRANSIENT";
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            focusChangeStr = "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK";
            break;
        default:
            focusChangeStr = "AUDIOFOCUS_UNKNOW";
            break;
    }

    Log.d(TAG, "onAudioFocusChange: " + focusChange + " - " + focusChangeStr);

    WritableMap data = Arguments.createMap();
    data.putString("eventText", focusChangeStr);
    data.putInt("eventCode", focusChange);
    sendEvent("onAudioFocusChange", data);
}
 
Example 11
Source File: VideoView.java    From ExoMedia with Apache License 2.0 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 12
Source File: AppRTCAudioManager.java    From imsdk-android with MIT License 4 votes vote down vote up
public void start(AudioManagerEvents audioManagerEvents) {
  LogUtil.d(TAG, "start");
  AppRTCUtils.checkIsOnMainThread(apprtcContext);
  if (amState == AudioManagerState.RUNNING) {
    LogUtil.e(TAG, "AudioManager is already active");
    return;
  }
  amState = AudioManagerState.RUNNING;

  // TODO(henrika): perhaps call new method called preInitAudio() here if UNINITIALIZED.

  LogUtil.d(TAG, "AudioManager starts...");
  this.audioManagerEvents = audioManagerEvents;

  // Store current audio state so we can restore it when stop() is called.
  savedAudioMode = audioManager.getMode();
  savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
  savedIsMicrophoneMute = audioManager.isMicrophoneMute();
  hasWiredHeadset = hasWiredHeadset();

  // Create an AudioManager.OnAudioFocusChangeListener instance.
  audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    // Called on the listener to notify if the audio focus for this listener has been changed.
    // The |focusChange| value indicates whether the focus was gained, whether the focus was lost,
    // and whether that loss is transient, or whether the new focus holder will hold it for an
    // unknown amount of time.
    // TODO(henrika): possibly extend support of handling audio-focus changes. Only contains
    // logging for now.
    @Override
    public void onAudioFocusChange(int focusChange) {
      String typeOfChange = "AUDIOFOCUS_NOT_DEFINED";
      switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
          typeOfChange = "AUDIOFOCUS_GAIN";
          break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
          typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT";
          break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
          typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE";
          break;
        case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
          typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK";
          break;
        case AudioManager.AUDIOFOCUS_LOSS:
          typeOfChange = "AUDIOFOCUS_LOSS";
          break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
          typeOfChange = "AUDIOFOCUS_LOSS_TRANSIENT";
          break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
          typeOfChange = "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK";
          break;
        default:
          typeOfChange = "AUDIOFOCUS_INVALID";
          break;
      }
      LogUtil.d(TAG, "onAudioFocusChange: " + typeOfChange);
    }
  };

  // Request audio playout focus (without ducking) and install listener for changes in focus.
  int result = audioManager.requestAudioFocus(audioFocusChangeListener,
      AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
  if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    LogUtil.d(TAG, "Audio focus request granted for VOICE_CALL streams");
  } else {
    LogUtil.e(TAG, "Audio focus request failed");
  }

  // Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
  // required to be in this mode when playout and/or recording starts for
  // best possible VoIP performance.
  audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

  // Always disable microphone mute during a WebRTC call.
  setMicrophoneMute(false);

  // Set initial device states.
  userSelectedAudioDevice = AudioDevice.NONE;
  selectedAudioDevice = AudioDevice.NONE;
  audioDevices.clear();

  // Initialize and start Bluetooth if a BT device is available or initiate
  // detection of new (enabled) BT devices.
  bluetoothManager.start();

  // Do initial selection of audio device. This setting can later be changed
  // either by adding/removing a BT or wired headset or by covering/uncovering
  // the proximity sensor.
  updateAudioDeviceState();

  // Register receiver for broadcast intents related to adding/removing a
  // wired headset.
  registerReceiver(wiredHeadsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
  LogUtil.d(TAG, "AudioManager started");
}
 
Example 13
Source File: AudioFocusRequest.java    From Fatigue-Detection with MIT License 4 votes vote down vote up
public void release() {
    mState = AudioManager.AUDIOFOCUS_GAIN_TRANSIENT;
    mFocusRequestChangeListeners.clear();
    mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
    mAudioManager = null;
}
 
Example 14
Source File: AppRTCAudioManager.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public void start(AudioManagerEvents audioManagerEvents) {
    Log.d(Config.LOGTAG, AppRTCAudioManager.class.getName() + ".start()");
    ThreadUtils.checkIsOnMainThread();
    if (amState == AudioManagerState.RUNNING) {
        Log.e(Config.LOGTAG, "AudioManager is already active");
        return;
    }
    awaitMicrophoneLatch();
    this.audioManagerEvents = audioManagerEvents;
    amState = AudioManagerState.RUNNING;
    // Store current audio state so we can restore it when stop() is called.
    savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
    savedIsMicrophoneMute = audioManager.isMicrophoneMute();
    hasWiredHeadset = hasWiredHeadset();
    // Create an AudioManager.OnAudioFocusChangeListener instance.
    audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        // Called on the listener to notify if the audio focus for this listener has been changed.
        // The |focusChange| value indicates whether the focus was gained, whether the focus was lost,
        // and whether that loss is transient, or whether the new focus holder will hold it for an
        // unknown amount of time.
        // TODO(henrika): possibly extend support of handling audio-focus changes. Only contains
        // logging for now.
        @Override
        public void onAudioFocusChange(int focusChange) {
            final String typeOfChange;
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    typeOfChange = "AUDIOFOCUS_GAIN";
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT";
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
                    typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE";
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK";
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    typeOfChange = "AUDIOFOCUS_LOSS";
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    typeOfChange = "AUDIOFOCUS_LOSS_TRANSIENT";
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    typeOfChange = "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK";
                    break;
                default:
                    typeOfChange = "AUDIOFOCUS_INVALID";
                    break;
            }
            Log.d(Config.LOGTAG, "onAudioFocusChange: " + typeOfChange);
        }
    };
    // Request audio playout focus (without ducking) and install listener for changes in focus.
    int result = audioManager.requestAudioFocus(audioFocusChangeListener,
            AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Log.d(Config.LOGTAG, "Audio focus request granted for VOICE_CALL streams");
    } else {
        Log.e(Config.LOGTAG, "Audio focus request failed");
    }
    // Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
    // required to be in this mode when playout and/or recording starts for
    // best possible VoIP performance.
    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    // Always disable microphone mute during a WebRTC call.
    setMicrophoneMute(false);
    // Set initial device states.
    userSelectedAudioDevice = AudioDevice.NONE;
    selectedAudioDevice = AudioDevice.NONE;
    audioDevices.clear();
    // Initialize and start Bluetooth if a BT device is available or initiate
    // detection of new (enabled) BT devices.
    bluetoothManager.start();
    // Do initial selection of audio device. This setting can later be changed
    // either by adding/removing a BT or wired headset or by covering/uncovering
    // the proximity sensor.
    updateAudioDeviceState();
    // Register receiver for broadcast intents related to adding/removing a
    // wired headset.
    registerReceiver(wiredHeadsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    Log.d(Config.LOGTAG, "AudioManager started");
}
 
Example 15
Source File: AppRTCAudioManager.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public void start(AudioManagerEvents audioManagerEvents) {
    Log.d(Config.LOGTAG, AppRTCAudioManager.class.getName() + ".start()");
    ThreadUtils.checkIsOnMainThread();
    if (amState == AudioManagerState.RUNNING) {
        Log.e(Config.LOGTAG, "AudioManager is already active");
        return;
    }
    awaitMicrophoneLatch();
    this.audioManagerEvents = audioManagerEvents;
    amState = AudioManagerState.RUNNING;
    // Store current audio state so we can restore it when stop() is called.
    savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
    savedIsMicrophoneMute = audioManager.isMicrophoneMute();
    hasWiredHeadset = hasWiredHeadset();
    // Create an AudioManager.OnAudioFocusChangeListener instance.
    audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        // Called on the listener to notify if the audio focus for this listener has been changed.
        // The |focusChange| value indicates whether the focus was gained, whether the focus was lost,
        // and whether that loss is transient, or whether the new focus holder will hold it for an
        // unknown amount of time.
        // TODO(henrika): possibly extend support of handling audio-focus changes. Only contains
        // logging for now.
        @Override
        public void onAudioFocusChange(int focusChange) {
            final String typeOfChange;
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    typeOfChange = "AUDIOFOCUS_GAIN";
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT";
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:
                    typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE";
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    typeOfChange = "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK";
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    typeOfChange = "AUDIOFOCUS_LOSS";
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    typeOfChange = "AUDIOFOCUS_LOSS_TRANSIENT";
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    typeOfChange = "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK";
                    break;
                default:
                    typeOfChange = "AUDIOFOCUS_INVALID";
                    break;
            }
            Log.d(Config.LOGTAG, "onAudioFocusChange: " + typeOfChange);
        }
    };
    // Request audio playout focus (without ducking) and install listener for changes in focus.
    int result = audioManager.requestAudioFocus(audioFocusChangeListener,
            AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Log.d(Config.LOGTAG, "Audio focus request granted for VOICE_CALL streams");
    } else {
        Log.e(Config.LOGTAG, "Audio focus request failed");
    }
    // Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
    // required to be in this mode when playout and/or recording starts for
    // best possible VoIP performance.
    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    // Always disable microphone mute during a WebRTC call.
    setMicrophoneMute(false);
    // Set initial device states.
    userSelectedAudioDevice = AudioDevice.NONE;
    selectedAudioDevice = AudioDevice.NONE;
    audioDevices.clear();
    // Initialize and start Bluetooth if a BT device is available or initiate
    // detection of new (enabled) BT devices.
    bluetoothManager.start();
    // Do initial selection of audio device. This setting can later be changed
    // either by adding/removing a BT or wired headset or by covering/uncovering
    // the proximity sensor.
    updateAudioDeviceState();
    // Register receiver for broadcast intents related to adding/removing a
    // wired headset.
    registerReceiver(wiredHeadsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    Log.d(Config.LOGTAG, "AudioManager started");
}
 
Example 16
Source File: VideoView.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Sets which type of audio focus will be requested during the playback, or configures playback
 * to not request audio focus. Valid values for focus requests are
 * {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT},
 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and
 * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use
 * {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be
 * requested when playback starts. You can for instance use this when playing a silent animation
 * through this class, and you don't want to affect other audio applications playing in the
 * background.
 * @param focusGain the type of audio focus gain that will be requested, or
 *    {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during playback.
 */
public void setAudioFocusRequest(int focusGain) {
    if (focusGain != AudioManager.AUDIOFOCUS_NONE
            && focusGain != AudioManager.AUDIOFOCUS_GAIN
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
            && focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) {
        throw new IllegalArgumentException("Illegal audio focus type " + focusGain);
    }
    mAudioFocusType = focusGain;
}