简体   繁体   中英

AudioFocusRequest.Builder() requires API level 26 but I want to use it on API level 22, is there any way to use it on API levels less than API 26?

I am developing a Media Player app and the min API level that I want it to support is API 22.

I want to use Audio Focus for the media playback, but the AudioFocusRequest.Builder() doesn't work on API levels lower than API 26 and the method requestAudioFocus() that used to work on API levels less than API 26 is deprecated now, so how can I use Audio Focus in the app so that it works on API levels less than API 26 and also on higher API levels as well?

audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                                .setOnAudioFocusChangeListener(audioFocusChangeListener)
                                .setFocusGain(AudioManager.AUDIOFOCUS_GAIN)
                                .build();

This is the piece of code that doesn't work on API levels less than 26 and gives this error message,

"Call requires API level 26 (current min is 22): new android.media.AudioFocusRequest.Builder"

Check the API level at runtime to choose whether to use requestAudioFocus() or AudioFocusRequest.Builder .

if (Build.VERSION.SDK_INT >= 26) {
  // Use AudioFocusRequest.Builder
} else {
  // Use requestAudioFocus
}

This should make both the error you received and the deprecation message clear from the IDE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM