Java Code Examples for android.media.AudioManager#AUDIOFOCUS_REQUEST_FAILED

The following examples show how to use android.media.AudioManager#AUDIOFOCUS_REQUEST_FAILED . 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: 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 2
Source File: MediaFocusControl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/** see AudioManager.dispatchFocusChange(AudioFocusInfo afi, int focusChange, AudioPolicy ap) */
int dispatchFocusChange(AudioFocusInfo afi, int focusChange) {
    if (DEBUG) {
        Log.v(TAG, "dispatchFocusChange " + focusChange + " to afi client="
                + afi.getClientId());
    }
    synchronized (mAudioFocusLock) {
        if (mFocusPolicy == null) {
            if (DEBUG) { Log.v(TAG, "> failed: no focus policy" ); }
            return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
        }
        final FocusRequester fr;
        if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
            fr = mFocusOwnersForFocusPolicy.remove(afi.getClientId());
        } else {
            fr = mFocusOwnersForFocusPolicy.get(afi.getClientId());
        }
        if (fr == null) {
            if (DEBUG) { Log.v(TAG, "> failed: no such focus requester known" ); }
            return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
        }
        return fr.dispatchFocusChange(focusChange);
    }
}
 
Example 3
Source File: AudioTrackManagerDualStreamType.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
public int abandonVRAudioFocus() {
    LogUtil.d(TAG, "abonden VR audio focus!");

    if (mAM != null) {
        return mAM.abandonAudioFocus(mVRAudioFocusListener);
    } else {
        return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    }
}
 
Example 4
Source File: TtsService.java    From android-app with GNU General Public License v3.0 5 votes vote down vote up
private void abandonAudioFocus() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        audioManager.abandonAudioFocus(audioFocusChangeListener);
    } else {
        if (audioFocusRequest != null && audioManager.abandonAudioFocusRequest(audioFocusRequest)
                != AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            audioFocusRequest = null;
        }
    }
    isAudioFocusGranted = false;
}
 
Example 5
Source File: MediaViewerActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Log.i(Config.LOGTAG, "Audio focus granted.");
    } else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
        Log.i(Config.LOGTAG, "Audio focus failed.");
    }
}
 
Example 6
Source File: AnnouncementPeriodicTask.java    From mytracks with Apache License 2.0 5 votes vote down vote up
/**
 * Speaks the announcement.
 * 
 * @param announcement the announcement
 */
private void speakAnnouncement(String announcement) {
  int result = audioManager.requestAudioFocus(
      null, TextToSpeech.Engine.DEFAULT_STREAM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
  if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
    Log.w(TAG, "Failed to request audio focus.");
  }

  /*
   * We don't care about the utterance id. It is supplied here to force
   * onUtteranceCompleted to be called.
   */
  tts.speak(announcement, TextToSpeech.QUEUE_FLUSH, SPEECH_PARAMS);
}
 
Example 7
Source File: AnnouncementPeriodicTask.java    From mytracks with Apache License 2.0 5 votes vote down vote up
@Override
public void onUtteranceCompleted(String utteranceId) {
  int result = audioManager.abandonAudioFocus(null);
  if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
    Log.w(TAG, "Failed to relinquish audio focus.");
  }
}
 
Example 8
Source File: InCallManagerModule.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
private void requestAudioFocus() {
    if (!isAudioFocused) {
        int result = audioManager.requestAudioFocus(mOnFocusChangeListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Log.d(TAG, "AudioFocus granted");
            isAudioFocused = true;
        } else if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            Log.d(TAG, "AudioFocus failed");
            isAudioFocused = false;
        }
    }
}
 
Example 9
Source File: AudioTrackManagerDualStreamType.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public int requestVRAudioFocus() {
    LogUtil.d(TAG, "request VR audio focus!");

    if (mAM != null) {
        // abandon Navi TTS audio focus
        mAM.abandonAudioFocus(mTTSAudioFocusListener);
        return mAM.requestAudioFocus(mVRAudioFocusListener, VehicleFactoryAdapter.getInstance()
                .getTTSAudioTrackStreamType(), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    } else {
        return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    }
}
 
Example 10
Source File: AudioTrackManagerDualNormal.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
public int abandonVRAudioFocus() {
    LogUtil.d(TAG, "abonden VR audio focus!");

    if (mAM != null) {
        return mAM.abandonAudioFocus(mVRAudioFocusListener);
    } else {
        return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    }
}
 
Example 11
Source File: AudioTrackManagerDualNormal.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public int requestVRAudioFocus() {
    LogUtil.d(TAG, "request VR audio focus!");

    if (mAM != null) {
        // abandon Navi TTS audio focus
        mAM.abandonAudioFocus(mTTSAudioFocusListener);
        return mAM.requestAudioFocus(mVRAudioFocusListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    } else {
        return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    }
}
 
Example 12
Source File: VehiclePCMPlayer.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
public int abandonVRAudioFocus() {
    if (mPriorityArbitrationModule == null) {
        return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    }

    return mPriorityArbitrationModule.abandonVRAudioFocus();
}
 
Example 13
Source File: FlutterIncallManagerPlugin.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
private void requestAudioFocus() {
    if (!isAudioFocused) {
        int result = audioManager.requestAudioFocus(mOnFocusChangeListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Log.d(TAG, "AudioFocus granted");
            isAudioFocused = true;
        } else if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            Log.d(TAG, "AudioFocus failed");
            isAudioFocused = false;
        }
    }
}
 
Example 14
Source File: ArbitrationModuleSingel.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
public int requestVRAudioFocus() {
    return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
}
 
Example 15
Source File: ArbitrationModuleSingel.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
public int abandonVRAudioFocus() {
    return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
}
 
Example 16
Source File: VehiclePCMPlayer.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
public int requestVRAudioFocus() {
    if (mPriorityArbitrationModule == null) {
        return AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    }
    return mPriorityArbitrationModule.requestVRAudioFocus();
}
 
Example 17
Source File: RNJWPlayerView.java    From react-native-jw-media-player with MIT License 4 votes vote down vote up
public void requestAudioFocus() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            if (hasAudioFocus) {
                return;
            }

            if (audioManager != null) {
                AudioAttributes playbackAttributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) // CONTENT_TYPE_SPEECH
                        .build();
                focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                        .setAudioAttributes(playbackAttributes)
                        .setAcceptsDelayedFocusGain(true)
//                    .setWillPauseWhenDucked(true)
                        .setOnAudioFocusChangeListener(this)
                        .build();

                int res = audioManager.requestAudioFocus(focusRequest);
                synchronized(focusLock) {
                    if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
                        playbackNowAuthorized = false;
                    } else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                        playbackNowAuthorized = true;
                        hasAudioFocus = true;
                    } else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
                        playbackDelayed = true;
                        playbackNowAuthorized = false;
                    }
                }
                Log.e(TAG, "audioRequest: " + res);
            }
        }
        else {
            int result = 0;
            if (audioManager != null) {
                if (hasAudioFocus) {
                    return;
                }

                result = audioManager.requestAudioFocus(this,
                        // Use the music stream.
                        AudioManager.STREAM_MUSIC,
                        // Request permanent focus.
                        AudioManager.AUDIOFOCUS_GAIN);
            }
            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                hasAudioFocus = true;
            }
            Log.e(TAG, "audioRequest: " + result);
        }
    }