How to set Audio Focus ChangeListener in Android API O?

前端 未结 5 2059
暗喜
暗喜 2021-02-15 20:29

Since requestAudioFocus(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint) is deprecated in API O , how do you set audio focus change list

5条回答
  •  不知归路
    2021-02-15 20:53

    What I have founded in official docs:

    Explicit requests for audio focus: Your service can submit a more fine-grained request for receiving device-wide audio focus by using the requestAudioFocus() method. Pass in an AudioFocusRequest object, which you create using AudioFocusRequest.Builder. In this builder class, you can specify the following options:

    The type of focus you wish to gain, such as AUDIOFOCUS_GAIN_TRANSIENT or AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK. Whether your service should continue more quietly or pause completely when another audio service takes over device focus. Whether your service can wait to gain focus until the device is ready.

    Note: When building your instance of AudioFocusRequest, if you indicate that your service can wait to produce sound by calling setAcceptsDelayedFocusGain(), you must also call setOnAudioFocusChangeListener() so that your service knows when it can begin producing sound.

    As @kelebro63 mentioned the following example shows how to use an AudioFocusRequest.Builder to build an AudioFocusRequest and request and abandon audio focus:

    mAudioManager = (AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
    mPlaybackAttributes = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_GAME)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build();
    mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
            .setAudioAttributes(mPlaybackAttributes)
            .setAcceptsDelayedFocusGain(true)
            .setOnAudioFocusChangeListener(mMyFocusListener, mMyHandler)
            .build();
    mMediaPlayer = new MediaPlayer();
    final Object mFocusLock = new Object();
    
    boolean mPlaybackDelayed = false;
    boolean mPlaybackNowAuthorized = false;
    
    // ...
    int res = mAudioManager.requestAudioFocus(mFocusRequest);
    synchronized(mFocusLock) {
        if (res == AUDIOFOCUS_REQUEST_FAILED) {
            mPlaybackNowAuthorized = false;
        } else if (res == AUDIOFOCUS_REQUEST_GRANTED) {
            mPlaybackNowAuthorized = true;
            playbackNow();
        } else if (res == AUDIOFOCUS_REQUEST_DELAYED) {
           mPlaybackDelayed = true;
           mPlaybackNowAuthorized = false;
        }
    }
    
    // ...
    @Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
                if (mPlaybackDelayed || mResumeOnFocusGain) {
                    synchronized(mFocusLock) {
                        mPlaybackDelayed = false;
                        mResumeOnFocusGain = false;
                    }
                    playbackNow();
                } 
                break;
            case AudioManager.AUDIOFOCUS_LOSS:
                synchronized(mFocusLock) {
                    mResumeOnFocusGain = false;
                    mPlaybackDelayed = false;
                }
                pausePlayback();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                synchronized(mFocusLock) {
                    mResumeOnFocusGain = true;
                    mPlaybackDelayed = false;
                }
                pausePlayback();
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                // ... pausing or ducking depends on your application
                break;
            }
        }
    }
    

    References:

    Audio Focus Enhancements

    Android O Features and APIs

提交回复
热议问题