Reputation: 995
I'm currently implementing an AudioHelper for a training app which can run some motivational speeches during your trainingsession. So basically if a speech is running, I want to duck my spotify audio so the speech is clearly heard. For Kitkat and Oreo (and higher) I've got this working (see code). But for Lollipop I can't find the right implementation. On developer.android.com they have written:
For apps that target Android 5.0 (API level 21) and later, audio apps should use AudioAttributes to describe the type of audio your app is playing. For example, apps that play speech should specify CONTENT_TYPE_SPEECH. source
But where do I start, what do I need to achieve the same results as for kitkat and oreo? (AudioFocusRequest.Builder() is not yet available for API 21 till API 26.)
Here is my code snippet:
/** Apps running Android 8.0 (API level 26) or greater should use the 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.
*/
@RequiresApi(Build.VERSION_CODES.O)
private fun loadAndPlayAudioForOreoAndHigher() {
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
val audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK).run {
setAudioAttributes(audioAttributes)
setAcceptsDelayedFocusGain(true)
setOnAudioFocusChangeListener(MediaHelper)
build()
}
val focusRequest = audioManager.requestAudioFocus(audioFocusRequest)
if(focusRequest == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaPlayer.setOnCompletionListener { audioManager.abandonAudioFocusRequest(audioFocusRequest) }
mediaPlayer.start()
}
}
/**
* For apps that target Android 5.0 (API level 21) and later, audio apps should use
* AudioAttributes to describe the type of audio your app is playing. For example,
* apps that play speech should specify CONTENT_TYPE_SPEECH.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun loadAndPlayAudioForLollipopAndHigher() {
// TODO implement audioFocusRequest() and abandonAudioFocusRequest()
}
/**
* Beginning with Android 2.2 (API level 8), apps manage audio focus by calling
* requestAudioFocus() and abandonAudioFocus(). Apps must also register an
* AudioManager.OnAudioFocusChangeListener with both calls in order to receive callbacks
* and manage their own audio level.
*/
@Suppress("DEPRECATION")
@RequiresApi(Build.VERSION_CODES.KITKAT)
private fun loadAndPlayAudioForKitkatAndHigher() {
val focusRequestResult = audioManager.requestAudioFocus(this, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
if(focusRequestResult == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mediaPlayer.setOnCompletionListener { audioManager.abandonAudioFocus(this) }
mediaPlayer.start()
}
}
Upvotes: 4
Views: 3289
Reputation: 37
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE);
} else {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
}
Upvotes: 1