Audio Focus
Android O – 26.0 :
https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_GRANTED
To avoid every music app playing at the same time, Android introduces the idea of audio focus. Only one app can hold audio focus at a time.
When your app needs to output audio, it should request audio focus. When it has focus, it can play sound. However, after you acquire audio focus you may not be able to keep it until you’re done playing. Another app can request focus, which preempts your hold on audio focus. If that happens your app should pause playing or lower its volume to let users hear the new audio source more easily.
→ Apps running
Android 8.0 (API level 26) or greater should use the
int
requestAudioFocus (AudioFocusRequest
focusRequest).requestAudioFocus()
method, which takes an AudioFocusRequest
parameter. The AudioFocusRequest
contains information about the audio context and capabilities of your
app. The system uses this information to manage the gain and loss of
audio focus automatically.
→ People generally using following list of Api’s in code , but found that are deprecated in Android O(Api level 26) . Even though no issues following functionalities should work as expected in devices:
1) Blue-tooth pairing was fine and able to listen audio.
2) focus gain and loss notification between the apps.
3) pause/resume audio behavior between the apps.
4) Pause audio while lock the screen.
5) Pause audio while app goes background.
6) headphones audio fine, but need to test in Bluetooth speakers , Bluetooth audio on Android Auto .
AudioManager Api changes:
a)
Android
26 deprecated
Api’s
using in
our App:
This method was deprecated
in API level 26. use
abandonAudioFocusRequest(AudioFocusRequest)
Abandon audio focus. Causes the previous focus
owner, if any, to receive focus.→
requestAudioFocus(AudioManager.OnAudioFocusChangeListener
l, int streamType, int durationHint) : Even-thought
it is deprecated no effect in our App.requestAudioFocus(AudioFocusRequest)
This method was deprecated in API level 26. Use
getDevices(int)
instead to list available audio devices.
b)
Newly
Added audio
Api’s
in Android 26:
→
AUDIOFOCUS_NONE
int AUDIOFOCUS_NONE : Used to indicate no audio focus has been gained or lost, or requested. Constant Value: 0 (0x00000000)
→
AUDIOFOCUS_REQUEST_DELAYEDint AUDIOFOCUS_REQUEST_DELAYED : A focus change request whose granting is delayed: the request was successful, but the requester will only be granted audio focus once the condition that prevented immediate granting has ended. SeeConstant Value: 2 (0x00000002)requestAudioFocus(AudioFocusRequest)andsetAcceptsDelayedFocusGain(boolean)
→ STREAM_ACCESSIBILITY :
int STREAM_ACCESSIBILITY
Used to identify the volume of audio streams for accessibility
prompts : Constant Value: 10 (0x0000000a)
→ getActivePlaybackConfigurations
List<AudioPlaybackConfiguration> getActivePlaybackConfigurations ()Returns the current active audio playback configurations of the device.
→ registerAudioPlaybackCallback
void registerAudioPlaybackCallback (AudioManager.AudioPlaybackCallback cb, Handler handler)Register a callback to be notified of audio playback changes through
AudioManager.AudioPlaybackCallback→
unregisterAudioPlaybackCallbackvoid unregisterAudioPlaybackCallback (AudioManager.AudioPlaybackCallback cb)Unregister an audio playback callback previously registered with
registerAudioPlaybackCallback(AudioPlaybackCallback,
Handler).
No comments:
Post a Comment