123
123

Reputation: 197

ExoPlayer won't keep audio focus when pausing the video

I'm using ExoPlayer in my Android app to play few tutorial videos, and when I pause the video the audio focus gets abandoned and the user is not able to control the media volume via the physical buttons of the device (because the audio focus has been lost the buttons now control the ringtone volume) and I want to keep the audio focus even if the user pauses the video, and I only want it to be lost if the user exits out of the tutorial.

I already tried setting the "exoPlayer.setAudioAttributes" to null which suppose to prevent exoplayer from using its default audio focus settings but when that option is set to null ExoPlayer won't play at all... (I also tried to set to it an actual AudioAttributes like in this post: https://medium.com/google-exoplayer/easy-audio-focus-with-exoplayer-a2dcbbe4640e but it didn't have any effect)

I also tried setting up AudioManager (the build-in one) and request audio focus but immediately everytime I set it its listener returns "AUDIOFOCUS_LOSS" (I tried setting it before setting ExoPlayer and after, and also I tried setting it again everytime the state of ExoPlayer changes, but every time it returned "AUDIOFOCUS_LOSS" right away.

Here's my code:

    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setUsage(C.USAGE_MEDIA)
            .setContentType(C.CONTENT_TYPE_MOVIE)
            .build();
    exoPlayer.setAudioAttributes(audioAttributes, true);

Upvotes: 2

Views: 2662

Answers (2)

Md.Tarikul Islam
Md.Tarikul Islam

Reputation: 1291

You have to use setAudioAttributes for focusing the Audio. Here is the code that works for my project:

 val audioAttributes: AudioAttributes = AudioAttributes.Builder()
            .setUsage(C.USAGE_MEDIA)
            .setContentType(C.CONTENT_TYPE_MOVIE)
            .build()
 
simpleExoPlayer.setAudioAttributes(audioAttributes, true)

If you want to know in detail you can read this article.

Upvotes: 1

123
123

Reputation: 197

The problem was that I was not setting the stream type for the activity and that's why the hardware buttons weren't controling the music volume when the video was paused (Credit goes to a reddit user for posting this solution over there)

activity.setVolumeControlStream(AudioManager.{INSERT YOUR STREAM TYPE})

Android docs: https://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)

Upvotes: 0

Related Questions