How to set Audio Focus ChangeListener in Android API O?

前端 未结 5 2061
暗喜
暗喜 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 21:02

    Another option is to use AudiomanagerCompat together with AudioAttributesCompat and AudioRequestCompat, all part of Jetpack (androidx) compatibility libraries: https://developer.android.com/reference/androidx/media/AudioManagerCompat

    In this way, you can use the same code for Android O+ and also for previous versions (without conditional checks on the API version. But one of the drawbacks of this approach is that you can't use: .setAcceptsDelayedFocusGain(true)

    If you decide for this approach, then you have to use requestAudioFocus(mAudioManager, mFocusRequest) and abandonAudioFocusRequest(mAudioManager, mFocusRequest) to obtain and release audio focus.

    Partial sample code (in Kotlin): (Note: if you do not use Timber for logging, replace it by the standard Log function)

    class MyClass : AudioManager.OnAudioFocusChangeListener {
    
        internal var audioManager: AudioManager? = null
        private var focusRequest: AudioFocusRequestCompat? = null
        private var playbackAttributes : AudioAttributesCompat? = null
    
        // Create AudioManager and other audio related objects
        audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager?
        playbackAttributes = AudioAttributesCompat.Builder()
                    .setUsage(AudioAttributesCompat.USAGE_MEDIA)
                    .setContentType(AudioAttributesCompat.CONTENT_TYPE_SPEECH)
                    .build()
    
        focusRequest = AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN)
                    .setAudioAttributes(playbackAttributes ?: return)
                    .setWillPauseWhenDucked(false)
                    .setOnAudioFocusChangeListener(this)
                    .build()
    
        // Request audio focus and check if it was granted to us
        if (requestAudioFocus(audioManager!!, focusRequest!!) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
             // We obtained audio focus
    
        }
        else {
            // We could not obtain audio focus
    
        }
    
        // And to remove audio focus
        if (audioManager != null && focusRequest != null)
                abandonAudioFocusRequest(audioManager!!, focusRequest!!)
    
    
    
        // Implementation of OnAudioFocusChangeListener
        override fun onAudioFocusChange(focusState: Int) {
    
            // Invoked when the audio focus of the system is updated.
            when (focusState) {
                AudioManager.AUDIOFOCUS_GAIN -> {
                    // We recovered audio focus.
                    Timber.d("Audio Focus obtained")
                }
                AudioManager.AUDIOFOCUS_LOSS -> {
                    // Lost focus for an unbounded amount of time
                    Timber.d("Audio Focus lost")
                }
                AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
                    // Lost focus for a short time, but it's ok to keep playing at an attenuated level
                    Timber.d("Audio Focus transient loss - can duck")
                }
                AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
                    // Lost focus for a short time
                    Timber.d("Audio Focus transient loss - cannot duck")
                }
            }
        }
    }
    

提交回复
热议问题