-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Comments
My current workaround is to add a listener and if 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)
}
}
}) |
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? |
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. |
Just checking I understand your question correctly:
If that summarizes your problem correctly, you should do three things:
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. |
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. |
It has a severe bug. |
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. |
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). |
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. |
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:
When the user toggles the mute state, I update the volume and update my audio attributes:
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.
The text was updated successfully, but these errors were encountered: