Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AudioFocus: Don't stop #5092

Closed
PaulWoitaschek opened this issue Nov 12, 2018 · 9 comments
Assignees

Comments

@PaulWoitaschek
Copy link

@PaulWoitaschek PaulWoitaschek commented Nov 12, 2018

I use ExoPlayer 2.9.0.
I have a video that should always be playing on a loop. By default this video plays muted. When the player clicks button, he can toggle if it should be muted.

My audio attributes look like this:

  private val audioAttributes = AudioAttributes.Builder()
    .setUsage(C.USAGE_MEDIA)
    .setContentType(C.CONTENT_TYPE_MOVIE)
    .build()

When the user toggles the mute state, I update the volume and update my audio attributes:

player.volume = if (muted) 0F else 1F
player.setAudioAttributes(audioAttributes, true)

However when the user now starts music, the video stops. Instead I need to customize the behavior to not stop the player but just mute it and give me a callback.

@PaulWoitaschek

This comment has been minimized.

Copy link
Author

@PaulWoitaschek PaulWoitaschek commented Nov 12, 2018

My current workaround is to add a listener and if plpayWhenReady is false, to mute the player, tell exoplayer to not handle the focus, set it playing and tell it to handle the focus again.

    player.addListener(object : Player.EventListener {
      override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
        if (!playWhenReady) {
          player.setAudioAttributes(audioAttributes, false)
          muted = true
          player.playWhenReady = true
          player.setAudioAttributes(audioAttributes, true)
        }
      }
    })
@tonihei

This comment has been minimized.

Copy link
Contributor

@tonihei tonihei commented Nov 12, 2018

Not sure if it's related to #5055. Would be great if you can try to update to 2.9.1 and see if the problem still persists.

Otherwise, it would be helpful to get more detailed reproduction steps. I'm currently struggling to understand what's happening exactly and in what order. Please also provide the state of the player (playWhenReady and playbackState) at the time you call setAudioAttributes and setPlayWhenReady.

Also, does the muting part make a difference? That is, does the problem still occur if you set the volume to 1.0 in all cases?

@tonihei tonihei self-assigned this Nov 12, 2018
@PaulWoitaschek

This comment has been minimized.

Copy link
Author

@PaulWoitaschek PaulWoitaschek commented Nov 12, 2018

In 2.9.1 it's different, sorry for reporting with an outdated version.

My solution is now to update exoplayer to handle the audio-focus based on if it's muted or not:

  var muted: Boolean
    set(muted) {
      player.volume = if (muted) 0F else 1F
      val shouldHandleAutoFocus = !muted
      player.setAudioAttributes(audioAttributes, shouldHandleAutoFocus)
    }

And then mute in the listener (which updates the player to not handle autofocus) and continue playback:

    player.addListener(object : Player.EventListener {
      override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
        if (!playWhenReady) {
          // if the player was paused due to audio focus, tell the player to not handle audiofocus, start playback muted and
          // resume playing.
          muted = true
          player.playWhenReady = true
        }
      }
    })

I'm not sure if there is a better solution to support this, feel free to close if you don't think so.

@tonihei

This comment has been minimized.

Copy link
Contributor

@tonihei tonihei commented Nov 12, 2018

Just checking I understand your question correctly:

  • You have an endlessly looping video with a "mute" button to turn volume on and off.
  • Current behaviour: Audio focus is handled by SimpleExoPlayer. So when some other app requests the focus, the player will pause (even if muted).
  • Intended behaviour: If the player is already muted, continue playing even if the audio focus is lost. And if player is not muted and the audio focus is lost, switched to muted and keep playing.

If that summarizes your problem correctly, you should do three things:

  • Disable audio focus handling if "mute" mode gets enabled.
  • Enable audio focus handling if "mute" mode gets disabled.
  • Enable mute mode and disable audio focus handling if audio focus is lost while "mute" mode is disabled.

That's what the code you posted above is doing already. However, the part you are currently doing in onPlayerStateChanged should move to a new callback which informs you of audio focus changes (so that you don't need to reenable playWhenReady). This is already asked for in #5087, so I'll use that issue to track this enhancement.

@tonihei

This comment has been minimized.

Copy link
Contributor

@tonihei tonihei commented Nov 19, 2018

I'll close this issue under the assumption the question was answered. #5087 is still open to track adding a more suitable callback you can use to update the muted sate.

@tonihei tonihei closed this Nov 19, 2018
@PaulWoitaschek

This comment has been minimized.

Copy link
Author

@PaulWoitaschek PaulWoitaschek commented Dec 5, 2018

That's what the code you posted above is doing already.

It has a severe bug.
I set setAudioAttributes with handleAudioFocus set to false.
After an incoming phonecall, the audio starts playing loud.

@tonihei

This comment has been minimized.

Copy link
Contributor

@tonihei tonihei commented Dec 5, 2018

Can you add some context and explain what happens exactly? If it's not the same issue as above, it may be worth starting a new issue.

@ojw28

This comment has been minimized.

Copy link
Contributor

@ojw28 ojw28 commented Dec 5, 2018

That's proabbly a duplicate of #3923 (which has nothing to do with audio focus; it's about the stream volume resetting after a phone call).

@PaulWoitaschek

This comment has been minimized.

Copy link
Author

@PaulWoitaschek PaulWoitaschek commented Dec 5, 2018

Looks like. As you can't fix it maybe you should add a warning that one shoudn't change the volume using exoplayer on post Android Q at all?

Setting the audio volume up after a user explicitly muted is very disturbing to the user.

@google google locked and limited conversation to collaborators May 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants
You can’t perform that action at this time.